Ask Your Question

stanislav's profile - activity

2023-06-28 12:25:18 +0200 received badge  Notable Question (source)
2018-08-30 08:25:57 +0200 received badge  Famous Question (source)
2018-05-21 14:55:42 +0200 received badge  Nice Question (source)
2018-01-11 22:16:54 +0200 received badge  Popular Question (source)
2015-05-29 20:00:25 +0200 received badge  Notable Question (source)
2014-02-11 06:58:34 +0200 received badge  Popular Question (source)
2014-01-10 15:43:02 +0200 received badge  Famous Question (source)
2013-11-18 13:48:01 +0200 received badge  Good Question (source)
2013-04-26 08:13:43 +0200 received badge  Famous Question (source)
2012-09-13 10:54:25 +0200 received badge  Notable Question (source)
2012-03-12 10:27:57 +0200 received badge  Notable Question (source)
2012-02-08 08:02:57 +0200 received badge  Popular Question (source)
2012-01-21 10:44:26 +0200 received badge  Taxonomist
2011-07-29 14:04:40 +0200 received badge  Popular Question (source)
2011-01-18 08:02:15 +0200 marked best answer solve multidimensional nonlinear system with scipy

It's worth mentioning that the scipy multidimensional optimizers like broyden2 are often very sensitive to initial conditions.

For example (and using the "f(*z)" syntax to turn the vector of arguments scipy.optimize gives into the arguments f needs:

sage: f(x,y) = (x^2+y, y^2-x+2)
sage: sol=vector(optimize.broyden2(lambda z: f(*z), [0., 0.])) # note 0. not 0, to get the coercion right
sage: sol
(-0.445049106949, 1.09664015201)
sage: f(*sol)
(1.29470885961, 3.64766872995)
sage: vector(f(*sol)).norm()
3.87062762283
sage: 
sage: for startloc in CartesianProduct([(-3.)..(0.)], [(-3.)..(0.)]):
....:    sol=vector(optimize.broyden2(lambda z: f(*z), startloc))
....:    print startloc, sol, f(*sol), vector(f(*sol)).norm()
....: 
[-3.00000000000000, -3.00000000000000] (0.592528157331, -0.832520486937) (-0.481430869706, 2.10056220384) 2.15502604497
[-3.00000000000000, -2.00000000000000] (0.284633829217, -0.299571581674) (-0.218555164939, 1.80510930333) 1.81829204395
[-3.00000000000000, -1.00000000000000] (1.77544832692, -0.455717154906) (2.69649960665, 0.432229798359) 2.73092158936
[-3.00000000000000, 0.000000000000000] (-0.262258372557, 2.46143841915) (2.53021787312, 8.32093746381) 8.69712612086
[-2.00000000000000, -3.00000000000000] (0.475497108447, 3.03104628731) (3.25714378745, 10.7117444873) 11.1960017691
[-2.00000000000000, -2.00000000000000] (-1.12430279872, -0.185723262141) (1.07833352106, 3.15879592882) 3.33778293221
[-2.00000000000000, -1.00000000000000] (0.0922121941783, 0.648230430052) (0.656733518807, 2.32799049627) 2.41885069102
[-2.00000000000000, 0.000000000000000] (-0.181200179841, 1.00534124397) (1.03817474914, 3.19191119667) 3.35650173502
[-1.00000000000000, -3.00000000000000] (-0.658431945525, -0.19952862808) (0.234003998808, 2.69824361895) 2.70837155846
[-1.00000000000000, -2.00000000000000] (-0.2997258552, 1.45003224177) (1.53986783005, 4.40231935738) 4.66386198963
[-1.00000000000000, -1.00000000000000] (-0.184337236075, 0.0620231427285) (0.0960033593323, 2.18818410631) 2.19028909692
[-1.00000000000000, 0.000000000000000] (3.07457749063, -4.84309303007) (4.60993371584, 22.3809726073) 22.8508079444
[0.000000000000000, -3.00000000000000] (2.08531742062, 8.58333035147) (12.9318790962, 73.5882425019) 74.7158813873
[0.000000000000000, -2.00000000000000] (-0.625399327611, 1.17293600206) (1.56406032104, 4.00117819254) 4.29601112851
[0.000000000000000, -1.00000000000000] (-1.64468007664, -1.36976452276) (1.33520803175, 5.52093492444) 5.6800970879
[0.000000000000000, 0.000000000000000] (-0.445049106949, 1.09664015201) (1.29470885961, 3.64766872995) 3.87062762283
# norms all over the place!

I would actually recommend using the higher-level 'minimize' function which calls some of the scipy routines rather than the internal scipy ones directly: it does a better job wrapping Sage-native objects. http://www.sagemath.org/doc/reference/sage/numerical/optimize.html

sage: f(x,y) = (x^2+y, y^2-x+2)
sage: sol = minimize(norm(f), [0., 0.]) # add disp=0 to get it to be quiet
Optimization terminated successfully.
         Current function value: 1.288361
         Iterations: 6
         Function evaluations: 9
         Gradient evaluations: 9
sage: sol
(0.935264263401, -0.26730415417)
sage: f(*sol)
(0.607415088225, 1.13618724744)
sage: vector(f(*sol)).norm()
1.28836118796

I'm too lazy to figure out whether that's the global minimum or not, but a quick brute force scan suggests it must have gotten pretty close. (IMHO we should probably wrap OpenOpt or something; optimization is an important enough problem to be worth doing well.)

Note that this ... (more)

2011-01-18 08:02:13 +0200 marked best answer solve multidimensional nonlinear system with scipy

Try this. does this help?

from scipy import optimize
def f(z):
    x=z[0]
    y=z[1]
    ans = (x^2+y,y^2-x+2)
    return(ans)
sol=optimize.broyden2(f,[0,0])
2011-01-18 08:01:53 +0200 answered a question solve multidimensional nonlinear system with scipy

Hello,

@ chicago: This variant working well but it is a little to long for bigger systems. Thank you of coarse.

@ DSM: it's really funny. I have read you answer this morning and I didn't known how to use minimize with a system of functions. But I hag a numeric lecture today where we have done this, so now I know, how to use minimize method with a system.

The variant with lambda function does not work for me. I get wrong solutions compared with the minimize method.

I think I will use minimize as often as I can ;) Thanks for quick replies.strong text

2011-01-18 04:52:56 +0200 received badge  Nice Question (source)
2011-01-17 17:49:03 +0200 asked a question solve multidimensional nonlinear system with scipy

Hello, I'm try to solve a nonlinear system of equitations numerically with sage. But I can not see some easy ways to do it.

All I have found is to use scipy.optimize library to do it, but I can not use this correctly with sage functions.

As a small example:

sage: f(x,y) = (x^2+y,y^2-x+2)
sage: optimize.broyden2(f,[0,0])
Traceback (click to the left of this block for traceback)
...
TypeError: no canonical coercion from <type 'tuple'> to Callable
function ring with arguments (x, y)

How shell I define my function f to use the broyden2 method well?

Or are there some other possibilities?

I thought that sage is a all in one solution with algebra and numeric math methods.

Can someone help me please.

2011-01-15 11:12:42 +0200 asked a question highlighting the code in notebook

I wonder if there is any way to highlight the code (things like "def" "for") in the notebook.

I have found some informations about the css fileused by notebook but maybe someone could describe me the way if there is one.

Thanks.

2011-01-15 07:44:57 +0200 answered a question Hide the Output

Thank you for quick response. I will use the assignment to the _ variable.

2011-01-15 07:09:19 +0200 marked best answer Hide the Output

This question was also asked here. The answer is to use assignment:

sage: 1+1
2
sage: a = 1+1
sage: _ = 1+1

The last example is probably the best one to use most of the time, since "_" generally represents the result of the last evaluation (like the ANS button on a calculator). So if you assign to "_", then you're not "wasting" a variable. You'll see this trick used a lot in Sage's documentation.

2011-01-15 03:01:13 +0200 received badge  Student (source)
2011-01-14 13:33:10 +0200 received badge  Scholar (source)
2011-01-14 13:33:10 +0200 marked best answer Evaluate with a data set?

I think you are looking for the subs() method of symbolic expressions.

sage: var('A B')
(A, B)
sage: data = {A:1,B:2}
sage: expr = A+B
sage: expr
A + B
sage: expr.subs(data)
3

This will substitute as a dictionary. You can also do

sage: expr.subs(A=1,B=2)
3

if you like that syntax. Does that help?

2011-01-14 13:29:45 +0200 asked a question Hide the Output

I have just an other question.

Is it possible to hide the output from an expression especially in notebook mode? The ";" does not working, as I have seen.

Is there a way to do so?

Thanks.

2011-01-14 13:02:36 +0200 received badge  Editor (source)
2011-01-14 13:02:19 +0200 answered a question Evaluate with a data set?

Thanks, the second variant is the more comfortable for me, because then I can use a list of sets and put it into a for loop.

I think, the is no better way in sage because the way, you can use it in maxima can not be used in python. In maxima you can give the data list after an expression just by typing a comma like:

A+B,data

But I'm glad, the way with subs is working fine.

2011-01-14 11:47:17 +0200 asked a question Evaluate with a data set?

Can sage evaluate an expression with a given data set like ev function in maxima?

For example something like:

    A,B = var('A,B')
    data = [A==1,B==2]
    A + B with data
        3
    A + B
        A + B

Thanks for help.