# HG changeset patch
# User Bobby Moretti <[EMAIL PROTECTED]>
# Date 1179852649 25200
# Node ID 5d423d8161906a30efe1b6837524724737303db2
# Parent  82e2148257968030ef5a6d2a5ee415aaf0927f28
implemented SR.numerical_approximation()

diff -r 82e214825796 -r 5d423d816190 sage/calculus/calculus.py
--- a/sage/calculus/calculus.py    Mon May 21 09:16:32 2007 -0700
+++ b/sage/calculus/calculus.py    Tue May 22 09:50:49 2007 -0700
@@ -647,6 +647,47 @@ class SymbolicExpression(RingElement):
         """
         return long(int(self))

+    def numerical_approximation(self, prec=53):
+        """
+        Get a numerical approximation of self as either a real or complex
+        number.
+
+        INPUT:
+            prec -- the precision (in bits), or a field in which to coerce
+
+        OUTPUT:
+            An RealNumber or ComplexNumber approximation of self with prec
bits
+            of precision.
+
+        EXAMPLES:
+            sage: cos(3).numerical_approximation()
+            -0.989992496600445
+
+            sage: cos(3).numerical_approximation(200)
+            -0.98999249660044545727157279473126130239367909661558832881409
+
+            sage: SR(i + 1).numerical_approximation(32)
+            1.00000000 + 1.00000000*I
+
+        ALIASES:
+            numerical_aproximation and numerical_approx are the same.
+
+        """
+        # make sure the field is of the right precision
+        try:
+            field = RealField(prec)
+        except TypeError:
+            field = prec
+
+        try:
+            approx = self._mpfr_(field)
+        except TypeError:
+            # try to return a complex result
+            approx = self._complex_mpfr_field_(ComplexField(field.prec()))
+
+        return approx
+
+    numerical_approx = numerical_approximation

     def _mpfr_(self, field):
         raise TypeError

~Bobby

On 5/21/07, William Stein <[EMAIL PROTECTED]> wrote:
>
>
> On 5/21/07, Bobby Moretti <[EMAIL PROTECTED]> wrote:
> > In the past, we've favored polluting the class namespace over lack of
> > clarity. For example, simplify_trig = trig_simplify, simplify_rational =
> > rational_simplify, etc. Would it be so bad to have, something like:
> >
> > def numerical_approximation(self, prec):
> >     try:
> >         field = RealField(prec)
> >     except TypeError:
> >         field = prec
> >     return self._mpfr_(field)
> >
> > in symbolic expression objects?
>
> Actually I like that since it's so clear.   Be careful to allow
> the answer to be in ComplexField if there is no coercion
> to RealField.
>
> William
>
> >
>


-- 
Bobby Moretti
[EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~----------~----~----~----~------~----~------~--~---

Attachment: 4551.patch
Description: Binary data

Reply via email to