Egad! I just got pummeled! This will take a while to absorb - thanks, Simon. Just starting on the learning curve...
On Aug 31, 6:04 am, Simon King <[email protected]> wrote: > Hi! > > On 31 Aug., 09:50, MathLynx <[email protected]> wrote: > > > Thanks! But the question remains, how do I force the addition 1/x + > > 1/y to give (x+y)/(x*y) ? > > I don't understand your question. Didn't you try to simply type it in? > > sage: R.<x,y> = QQ[] > sage: 1/x+1/y > (x + y)/(x*y) > > Aha! It seems that you did not start with polynomials but with > symbolic variables, that only *look* like polynomials. > sage: var('x y') > (x, y) > sage: 1/x+1/y > 1/x + 1/y > > It really depends on your application whether it is better to use > polynomials or symbolic expressions. In one application, symbolic > expressions might be totally inefficient, in another application > polynomials might not provide all necessary functionality. > > Anyway. If you want to simplify the above symbolic expression, you can > use various simplification methods: > sage: p = 1/x+1/y > sage: p > 1/x + 1/y > sage: p.simplify_rational() > (x + y)/(x*y) > > See below on how to find all the simplification methods, e.g., by > <tab> completion. > > > Also, where do I find such a command as fraction_field? > > First, let me point out that in the first example above, the fraction > field is automatically created when you have a division by an element > of R (which in the example is a polynomial ring with two variables > over the rationals). So, perhaps there is no need that *you* > explicitly construct that ring? > > When you want to find a certain functionality, you have a lot of > options in Sage. > > 1. If you have a guess how a function might be called, you can simply > search the references. > > Searching "rational functions" onhttp://www.sagemath.org/search.html > gives many answers, among them: > *http://www.sagemath.org/doc/reference/sage/rings/fraction_field_eleme..., > telling you that you may create a fraction field in functional > notation, by FractionField(PolynomialRing(QQ,'x')) > *http://www.sagemath.org/doc/constructions/polynomials.html#evaluation..., > which also implies how to construct a rational function without > explicitly creating the fraction field. > > 2. You can also search the Sage sources. > a) There is the function "search_def", that searches for the names > occuring in definitions. In that case (searching "rational function" > or "fraction field") it did not return anything useful. But you can > guess that Python programmers would call a function "fraction_field" > or "FractionField". Thus: > > sage: search_def("fraction_field") > rings/integer_ring.pyx:689: def fraction_field(self): > rings/ring.pyx:1128: def fraction_field(self): > rings/ring.pyx:1157: def _pseudo_fraction_field(self): > rings/ring.pyx:1899: def fraction_field(self): > rings/ring.pyx:1921: def _pseudo_fraction_field(self): > rings/infinity.py:487: def fraction_field(self): > rings/infinity.py:785: def fraction_field(self): > rings/ring.pxd:11: cdef public object __fraction_field > rings/power_series_ring.py:1017: def fraction_field(self): > rings/polynomial/polynomial_ring.py:1726: def fraction_field(self): > rings/polynomial/polynomial_ring.py:1749: def fraction_field(self): > rings/finite_rings/integer_mod_ring.py:504: def > _pseudo_fraction_field(self): > rings/padics/padic_extension_generic.py:216: def > fraction_field(self, print_mode=None): > rings/padics/padic_base_generic.py:45: def fraction_field(self, > print_mode=None): > rings/padics/padic_base_leaves.py:407: def fraction_field(self, > print_mode = None): > rings/number_field/order.py:698: def fraction_field(self): > > sage: search_def("FractionField") > ext/interactive_constructors_c.pyx:115:def FractionField(*args, > **kwds): > rings/fraction_field_element.pyx:49:def is_FractionFieldElement(x): > rings/fraction_field_element.pyx:66:cdef class > FractionFieldElement(FieldElement): > rings/fraction_field.py:87:def FractionField(R, names=None): > rings/fraction_field.py:140:def is_FractionField(x): > rings/contfrac.py:958:def ContinuedFractionField(): > rings/polynomial/polynomial_element.pyx:35:cdef is_FractionField, > is_RealField, is_ComplexField > > 3. a) Tab completion is also a very helpful tool when you have a guess > how a method is called. Recall that Sage's underlying language is > Python. Usually, functionality provided by an object is available by > methods of that object. If you have a guess on how the method name may > start with, you simply start to write and then hit the <tab> key. > > sage: R = QQ['x','y'] > sage: R.frac<tab> > yields R.fraction_field as only completion. > > Back to the symbolic expression p defined above. You wanted to > simplify the sum of fractions. So, a reasonable guess is that you need > a method whose name starts with "simpli". You find: > sage: p.simpli< hit tab key> > p.simplify p.simplify_factorial p.simplify_log > p.simplify_rational > p.simplify_exp p.simplify_full p.simplify_radical > p.simplify_trig > sage: p.simplify > > Again, you may pick what sounds appealing to you. Option 4 below tells > you how to learn about the methods found by tab completion. > > 3. b) You can get a list of all available methods by Python's dir() > function: > sage: dir(R) > yields a long list of available functionality, and you may pick what > sounds relevant to you. > > 4. Doc strings. > If you already have a method name that presumably does what you want, > then you may read its documentation. Simply type "?" after the method > and hit return: > > sage: R.fraction_field? > Type: builtin_function_or_method > Base Class: <type 'builtin_function_or_method'> > String Form: <built-in method fraction_field of > sage.rings.polynomial.multi_polynomial_libsingular.MPolynomialRing_libsingu > lar > object at 0x1c0cb30> > Namespace: Interactive > Definition: R.fraction_field(self) > Docstring: > > Return the fraction field of self. > > EXAMPLES: > > sage: R = Integers(389)['x,y'] > sage: Frac(R) > Fraction Field of Multivariate Polynomial Ring in x, y over > Ring of integers modulo 389 > sage: R.fraction_field() > Fraction Field of Multivariate Polynomial Ring in x, y over > Ring of integers modulo 389 > > 5. Source code. > You may also read the source code (in an interactive session). For > example, obviously a rational function is obtained by dividing > polynomials. Since you know Python, you also know that division of an > element is implemented in a method called "__div__". Hence, you take a > polynomial and inspect its __div__, by appending "??" and hitting > return: > > sage: x.__div__?? > Type: method-wrapper > Base Class: <type 'method-wrapper'> > String Form: <method-wrapper '__div__' of > sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular > object at 0x5acc5a8> > Namespace: Interactive > Definition: x.__div__(self, right) > Source: > def __div__(self, right): > """ > Top-level multiplication operator for ring elements. > See extensive documentation at the top of element.pyx. > """ > if have_same_parent(self, right): > if (<RefPyObject *>self).ob_refcnt < inplace_threshold: > return (<RingElement>self)._idiv_(<RingElement>right) > else: > return (<RingElement>self)._div_(<RingElement>right) > global coercion_model > return coercion_model.bin_op(self, right, div) > > As you may guess, thing start to become messier, since now you have to > chase your way through the code. However, you could now inspect > "_div_" with single underscore, and find > > sage: x._div_?? > Type: builtin_function_or_method > Base Class: <type 'builtin_function_or_method'> > String Form: <built-in method _div_ of > sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular > object at 0x5acc5a8> > ... > ... > else: > return > (<MPolynomialRing_libsingular>left._parent).fraction_field() > (left,right) > > And in the last line, you see the fraction_field method mentioned. > > Of course, solution 5 is more addressed to expert users, but to my > experience it is a very efficient method to understand an algorithm. > > Best regards, > Simon -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
