[sage-support] numerical approximation with units of measurement?

2014-06-05 Thread Hal Snyder
Is there a preferred way of taking numerical approximation of a quantity in 
Sage with units of measure? Here is a contrived example:

r = 123/47 * units.length.meter
r
area = pi * r^2
area

output:

123/47*meter
15129/2209*pi*meter^2

Now imagine that r is not a literal but the result of other calculations 
involving units of measure, so you can't easily apply n() at the time of 
assignment to r. Is there a simple way to take n() of things without 
getting into the following?

# n(r)
n(r.coeffs()[0][0]^r.coeffs()[0][1])*r.args()[0]
==
2.61702127659574*meter

# n(area)
(n(area.coeffs()[0][0])*(area.args()[0])^area.coeffs()[0][1])
==
21.5161409036487*meter^2

###

-- 
You received this message because you are subscribed to the Google Groups 
sage-support group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


[sage-support] Numerical approximation of ceiling functions?

2013-04-23 Thread Kenneth Lin
Hi Sage,

I'm not sure if it's that I'm not doing this right, but I have this 
function that has a ceiling in it. I defined it like so:

botrk(h0_prime, a0, s0, c0) = h0_prime / ceil(log(20 * (a0 + 25) / (h0_prime 
+ 20 * (a0 + 25)), 0.95)) * (s0 + 0.4) * (1 + c0)

But it won't do approximations of the ceiling, only returning another 
symbolic expression that can't be approximated.

sage: botrk(3000, 10, 1, 0.1)
4620.000/ceil(-19.4957257462237*log(7/37))
sage: botrk(3000, 10, 1, 0.1).n()
---
TypeError Traceback (most recent call last)
ipython-input-63-503406e1b435 in module()
 1 botrk(Integer(3000), Integer(10), Integer(1), RealNumber('0.1')).n()

/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression.so 
in sage.symbolic.expression.Expression._numerical_approx 
(sage/symbolic/expression.cpp:21011)()

TypeError: cannot evaluate symbolic expression numerically


Does anyone know how to approximate ceilings? For my purpose, I could just 
plug this in again and get a result, but I was hoping for a better, cleaner 
way of doing it.

-- 
You received this message because you are subscribed to the Google Groups 
sage-support group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




[sage-support] Numerical approximation of symbolic coefficients

2012-02-05 Thread Michael Orlitzky
I've got these polynomials in two variables, `x`, and `u`. The 
polynomials are low degree (eight at the moment), but I'm working 
symbolically, so they print exactly:


  ..+ 314069483520)*sqrt(3) - 80295755776*x + 4831838208)/(1953125*x^63
- 73828125*x^61...

All I would really like is to see these displayed with approximate 
coefficients, and to compute their roots.


First attempt: loop through each term and try to n() the coefficient. 
Madness.


Second attempt: leave SR and work in `Polynomial`s over RR. This works 
for display purposes, but once I have an MPolynomial_polydict object, I 
can't figure out how to get the roots (potentially in either variable).


I can go back to SR, take the roots, and then.. go back to Polynomials? 
Is there a better way to go about it? I feel like I'm reinventing the wheel.


--
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org


Re: [sage-support] Numerical approximation of symbolic coefficients

2012-02-05 Thread D. S. McNeil
 First attempt: loop through each term and try to n() the coefficient.
 Madness.

Based on a suggestion Mike Hansen once gave me --
http://ask.sagemath.org/question/411/substituting-expressions-for-numbers
-- I tend to use subclasses of Converter when I need to do something
like this, so as not to get lost in the madness. :^)  Something like:


from sage.symbolic.expression_conversions import Converter

class Evaluator(Converter):
def arithmetic(self, ex, operator):
return reduce(operator, map(self, ex.operands()))
def pyobject(self, ex, obj):
return ex.n()

sage: E = Evaluator()
sage: var(u x)
(u, x)
sage: q = ((314069483520)*sqrt(3/(sin(u+2)))*u - 80295755776*x +
4831838208)/(1953125*x^63)
sage: q
33554432/1953125*(9360*sqrt(3)*u*sqrt(1/sin(u + 2)) - 2393*x + 144)/x^63
sage: E(q)
17.17986918398*(16211.99555884469*u*(1/sin(u + 2.0))^0.5 -
2393.0*x + 144.0)/x^63.0



Doug

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org


[sage-support] numerical approximation in sage.

2009-11-27 Thread Yotam Avital
Hello.

In the tutorials there is an example for numerical approximation:

var('x y p q')
(x, y, p, q)
eq1 = p+q==9
eq2 = q*y+p*x==-6
eq3 = q*y^2+p*x^2==24
solns = solve([eq1,eq2,eq3,p==1],p,q,x,y, solution_dict=True)
[[s[p].n(30), s[q].n(30), s[x].n(30), s[y].n(30)] for s in solns]
[[1.000, 8.000, -4.8830369, -0.13962039],
 [1.000, 8.000, 3.5497035, -1.1937129]]


As I far as I can understand, solution_dict tells sage that I want the
output to be in dictionary form(that is, {x:1, y:8 ...})
I also know that the .n(30) tell sage I want the answer to have 30
digits accuracy. I can't understand though the logic of the last
command. Can any of you explain it to me?

Thanks.

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Re: [sage-support] numerical approximation in sage.

2009-11-27 Thread Minh Nguyen
Hi Yotam,

On Sat, Nov 28, 2009 at 5:03 AM, Yotam Avital yota...@gmail.com wrote:

SNIP

 As I far as I can understand, solution_dict tells sage that I want the
 output to be in dictionary form(that is, {x:1, y:8 ...})

Yes, you're right.


 I also know that the .n(30) tell sage I want the answer to have 30 digits
 accuracy.

Not quite. The method .n(30) tells Sage that you want an approximation
with 30 bits of precision. The default is to use 53 bits of precision.
This method has two keywords: prec for specifying the number of bits
of precision; and digits for specifying the number of digits. For
example, consider the following Sage session:

{{{
[mv...@sage ~]$ sage
--
| Sage Version 4.2.1, Release Date: 2009-11-14   |
| Type notebook() for the GUI, and license() for information.|
--
sage: var('x, y, p, q');
sage: eq1 = p + q == 9
sage: eq2 = q*y + p*x == -6
sage: eq3 = q*y^2 + p*x^2 == 24
sage: solns = solve([eq1, eq2, eq3, p==1], p, q, x, y, solution_dict=True)
sage: sol = solns[0][p]
sage: # approximate sol with 30 bits of precision
sage: sol.n(30)
1.000
sage: sol.n(prec=30)
1.000
sage: # approximate sol with 30 digits of precision
sage: sol.n(digits=30)
1.0
}}}

You can find out more about the method .n() by issuing sol.n? at the
command line.


 I can't understand though the logic of the last command. Can any
 of you explain it to me?

You were asking for an approximation with 30 bits of precision. If you
want an approximation with 30 digits of precision, use the key word
digits of the method .n().

-- 
Regards
Minh Van Nguyen

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org


[sage-support] Numerical approximation

2009-05-02 Thread Fidel


Hello,

I think I tried to post this about an hour ago, but the discussion
didn't show up. So I'm doing it again, sorry in case it is repeated.

I am working in sage 3.4.1

I am trying to define a function to get the LaTeX string of a graph,
so I am trying to convert a number to string.

I have x=6.1230317691118863e-17, which I got from

sage: x=graphs.PetersenGraph().get_pos()[0][0]

I would like a two decimal approximation of x, that is 0.00.

I have tried

sage: numerical_approx(x,digits=2)
6.1e-17
sage: n(x,digits=2)
6.1e-17

Is there a way to get 0.00 as output?

Thanks in advance for your attention and help.

Greetings,
Fidel

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~--~~~~--~~--~--~---