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

2014-06-06 Thread Nils Bruin
On Thursday, June 5, 2014 6:32:42 PM UTC-7, Hal Snyder wrote:

 IIs there a simple way to take n() of things without getting into the 
 following?

You could automate the application, but you'll quickly see you need to be a 
bit careful:

#unfortunately, the operators returned for sums and products of multiple
#arguments are callable, but don't accept multiple arguments, so we need to
#do a little surgery ourselves (borrow the functionality from elsewhere):
 
opdict = {
  operator.mul : sage.interfaces.maxima_lib.mul_vararg,
  operator.add : sage.interfaces.maxima_lib.add_vararg,
}
def recn(e):
try:
return n(e)
except TypeError:
pass
op=e.operator()
if op:
if op in opdict:
  op = opdict[op]
return op(*[recn(c) for c in e.operands()])
else:
return e
--

This now works, a little bit:

sage: recn(area)
21.5161409036487*meter^2.00

As you can see, the exponent in meter^2 was also numerified. Perhaps you 
didn't want that?

Nonetheless, a recursive n(..) method seems eminently reasonable and 
desirable to implement.

-- 
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] Re: numerical approximation with units of measurement?

2014-06-06 Thread Hal Snyder
Thank you! I have a lot to learn about Sage, I can see. Will study  
experiment with recn.

In most cases, an integer exponent should look like 2 and not 
2.000, don't you think? So I guess I would not n() the exponents.

I have just started using Sage and SMC with some online classes. It's great 
to:
- check work with units of measure
- take notes that look like math rather than COBOL (+1 typeset_mode)
- do symbolic and numerical calculations in those same typeset notes

I'm hooked. :-)

On Friday, June 6, 2014 6:53:18 PM UTC-5, Nils Bruin wrote:

 On Thursday, June 5, 2014 6:32:42 PM UTC-7, Hal Snyder wrote:

 IIs there a simple way to take n() of things without getting into the 
 following?

 You could automate the application, but you'll quickly see you need to be 
 a bit careful:

 #unfortunately, the operators returned for sums and products of multiple
 #arguments are callable, but don't accept multiple arguments, so we need to
 #do a little surgery ourselves (borrow the functionality from elsewhere):
  
 opdict = {
   operator.mul : sage.interfaces.maxima_lib.mul_vararg,
   operator.add : sage.interfaces.maxima_lib.add_vararg,
 }
 def recn(e):
 try:
 return n(e)
 except TypeError:
 pass
 op=e.operator()
 if op:
 if op in opdict:
   op = opdict[op]
 return op(*[recn(c) for c in e.operands()])
 else:
 return e
 --

 This now works, a little bit:

 sage: recn(area)
 21.5161409036487*meter^2.00

 As you can see, the exponent in meter^2 was also numerified. Perhaps you 
 didn't want that?

 Nonetheless, a recursive n(..) method seems eminently reasonable and 
 desirable to implement.


-- 
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] Re: Numerical approximation of ceiling functions?

2013-04-23 Thread P Purkayastha

On 04/21/2013 09:50 AM, Kenneth Lin wrote:

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.


Don't know why it stumbles on the ceiling. A function works on the other 
hand, since it avoids symbolics.


sage: def botrk(h0_prime, a0, s0, c0): return  h0_prime / ceil(log(20 * 
(a0 + 25) / (h0_prime + 20 * (a0 + 25)), 0.95)) * (s0 + 0.4) * (1 + c0)

:
sage: botrk(3000, 10, 1, 0.1)
140.



--
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] Re: numerical approximation in sage.

2009-11-27 Thread John H Palmieri
On Nov 27, 10:03 am, Yotam Avital yota...@gmail.com wrote:
 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?

If you're asking about the command

[[s[p].n(30), s[q].n(30), s[x].n(30), s[y].n(30)] for s in solns]

then note first that solns is a list, and a construction like [blah
for s in solns] evaluates blah for each entry s in solns. If you
just print solns at this point, you should get

[{q: 8, x: -4/3*sqrt(10) - 2/3, p: 1, y: 1/6*sqrt(2)*sqrt(5) - 2/3},
 {q: 8, x: 4/3*sqrt(10) - 2/3, p: 1, y: -1/6*sqrt(2)*sqrt(5) - 2/3}]

Each entry s in solns is a dictionary with keys the variables p, q, x,
y.  For the first entry, s[p] is 1, s[q] is 8, etc.  So the command
that I think you were asking about prints s[p], s[q], s[x], and s[y],
each with 30 bits of precision, for each of the two solutions.

--
John

-- 
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] Re: numerical approximation in sage.

2009-11-27 Thread Yotam Avital
My question is about the syntax and why does this syntax give a numerical
approximation.

To my understanding, solns is contracted from two arrays with  p,q,x,y being
the keys (because there are two solutions to the equations set). The part for
s in solns is putting in s ab array, and the part s[blah].n(30) asks sage
to give s[blah] in 30 bits precision.

Here is what I don't understand:

   - Is this syntax equivalent to:

for s in solns
s[q].n(30)
s[p].n(30)
s[x].n(30)
s[y].n(30)

   - Why does this method give a numerical solution.

Thanks.

On Fri, Nov 27, 2009 at 10:46 PM, John H Palmieri jhpalmier...@gmail.comwrote:

 On Nov 27, 10:03 am, Yotam Avital yota...@gmail.com wrote:
  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?

 If you're asking about the command

 [[s[p].n(30), s[q].n(30), s[x].n(30), s[y].n(30)] for s in solns]

 then note first that solns is a list, and a construction like [blah
 for s in solns] evaluates blah for each entry s in solns. If you
 just print solns at this point, you should get

 [{q: 8, x: -4/3*sqrt(10) - 2/3, p: 1, y: 1/6*sqrt(2)*sqrt(5) - 2/3},
  {q: 8, x: 4/3*sqrt(10) - 2/3, p: 1, y: -1/6*sqrt(2)*sqrt(5) - 2/3}]

 Each entry s in solns is a dictionary with keys the variables p, q, x,
 y.  For the first entry, s[p] is 1, s[q] is 8, etc.  So the command
 that I think you were asking about prints s[p], s[q], s[x], and s[y],
 each with 30 bits of precision, for each of the two solutions.

 --
 John

 --
 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


-- 
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] Re: numerical approximation in sage.

2009-11-27 Thread kcrisman
Hi,

This is an unavoidable consequence of using Maxima's solve commands, I
think - with multiple equations, Maxima's solve uses things like
algsys, if I'm not mistaken, and those return real solutions if they
can't find symbolic ones.

With one equation the (new) behavior is to not do this automatically
(the to_poly_solve=True option would allow this), but I don't think
there is any way to avoid the possibility of this with more than one
equation.  The documentation should be clear on this in the newest
version of Sage - I hope!

- kcrisman

-- 
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] Re: Numerical approximation

2009-05-02 Thread Utpal Sarkar

sage: round(x,2)
0.0
To show it really does what you want:
sage: round(1.2345,2)
1.23


On May 2, 8:35 pm, Fidel fidel.barr...@gmail.com wrote:
 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
-~--~~~~--~~--~--~---