Why is this Value Error raised?

62 views
Skip to first unread message

smoha...@gmail.com

unread,
Oct 30, 2014, 12:52:16 PM10/30/14
to sage-s...@googlegroups.com
#In The Name of God#
Hello to every one. I need to solve a large system of first order ODE's:(i is imaginary unit)
i*y'[t]=A[t].y[t]
to find PSI[t](a vector of n functions of t), given PSI[0]and A[t] (a sparse matrix with linear terms of t as non-zero elements).
As a begginer Sage user, I tried to follow instructions in "Solving ODE numerically by GSL" entry in Sage Documentation; for a small instance of my system my code is as follows:

var('s','t','T')
A=matrix(A_elements_dict);

f=A(s=t/T)

reset('i');
reset('x');

def Sch(t,y):
    return [ -i*sum(f[j,k]*y[j] for k in range(2^L)) for j in range(2^L)] 

NL1=[1 for i in range(2^L)];

TS=ode_solver()
TS.algorithm = "rk4"
TS.function=Sch
TS.y_0 = NL1
TS.ode_solve(t_span=[0,T], num_points=100)
print TS.solution;

But this error raised:

 File "/tmp/tmpwWuHMa/___code___.py", line 128, in <module>
    TS.ode_solve(t_span=[_sage_const_0 ,T], num_points=_sage_const_100 )
  File "ode.pyx", line 539, in sage.gsl.ode.ode_solver.ode_solve (build/cythonized/sage/gsl/ode.c:5053)
ValueError: error solving


However,since this syntax results in (trivial) answer when I set A[t]=0, I interpret that it means my code is correct (without syntax errors or loss of predefinitions).
Can anyone help me to find out the bug?
As I stated before, I am still a begginer in Sage.

William Stein

unread,
Oct 30, 2014, 1:14:27 PM10/30/14
to sage-support
On Thu, Oct 30, 2014 at 9:52 AM, <smoha...@gmail.com> wrote:
> #In The Name of God#
> Hello to every one. I need to solve a large system of first order ODE's:(i
> is imaginary unit)
> i*y'[t]=A[t].y[t]
> to find PSI[t](a vector of n functions of t), given PSI[0]and A[t] (a sparse
> matrix with linear terms of t as non-zero elements).
> As a begginer Sage user, I tried to follow instructions in "Solving ODE
> numerically by GSL" entry in Sage Documentation; for a small instance of my
> system my code is as follows:
>
> var('s','t','T')
> A=matrix(A_elements_dict);

NameError: name 'A_elements_dict' is not defined

>
> f=A(s=t/T)
>
> reset('i');
> reset('x');
>
> def Sch(t,y):
> return [ -i*sum(f[j,k]*y[j] for k in range(2^L)) for j in range(2^L)]
>
> NL1=[1 for i in range(2^L)];
>
> TS=ode_solver()
> TS.algorithm = "rk4"
> TS.function=Sch
> TS.y_0 = NL1
> TS.ode_solve(t_span=[0,T], num_points=100)
> print TS.solution;
>
> But this error raised:
>
> File "/tmp/tmpwWuHMa/___code___.py", line 128, in <module>
> TS.ode_solve(t_span=[_sage_const_0 ,T], num_points=_sage_const_100 )
> File "ode.pyx", line 539, in sage.gsl.ode.ode_solver.ode_solve
> (build/cythonized/sage/gsl/ode.c:5053)
> ValueError: error solving
>
>
> However,since this syntax results in (trivial) answer when I set A[t]=0, I
> interpret that it means my code is correct (without syntax errors or loss of
> predefinitions).
> Can anyone help me to find out the bug?
> As I stated before, I am still a begginer in Sage.
>
> --
> 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...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.



--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

smoha...@gmail.com

unread,
Oct 30, 2014, 8:23:33 PM10/30/14
to sage-s...@googlegroups.com
Dear Dr. William,
Thanks for your attention, but what you stated is not really my problem. Since the code above is just a part of my whole code, the software also does not reflects such 'NameError' to me. Moreover, as I stated, it even works without any errors when A[t] equals zero. By using 'A_elements_dict', I just want to illustrate that 'A' is a sparse matrix, which its non-zero elements are determined by a dictionary, and are linear terms of 't'.
What I (maybe!) need to know is
1) Why this error raised at all, since (despite usual) sage does not provide any more description for the error.
2) For such an ODE systems, is ode_solver() an appropriate choice?
I hope I could've stated what I mean!! Thank you again,
Yours Truly,
SMH

William Stein

unread,
Oct 30, 2014, 8:25:54 PM10/30/14
to sage-support
On Thu, Oct 30, 2014 at 5:23 PM, <smoha...@gmail.com> wrote:
> Dear Dr. William,
> Thanks for your attention, but what you stated is not really my problem.

Oh, sorry, it is my problem. It's hard for me to help you if you
don't provide code that 100% trivially works until the point where you
have trouble.

> Since the code above is just a part of my whole code, the software also does
> not reflects such 'NameError' to me. Moreover, as I stated, it even works
> without any errors when A[t] equals zero. By using 'A_elements_dict', I just
> want to illustrate that 'A' is a sparse matrix, which its non-zero elements
> are determined by a dictionary, and are linear terms of 't'.
> What I (maybe!) need to know is
> 1) Why this error raised at all, since (despite usual) sage does not provide
> any more description for the error.
> 2) For such an ODE systems, is ode_solver() an appropriate choice?
> I hope I could've stated what I mean!! Thank you again,
> Yours Truly,
> SMH
>

smoha...@gmail.com

unread,
Oct 30, 2014, 8:32:34 PM10/30/14
to sage-s...@googlegroups.com
I'm so sorry, it seems there are some typos I should edit:
... I need to solve a large system of first order ODE's:(i is imaginary unit)
i*y'[t]=A[t].y[t]
to find y[t] (a vector of n functions of t), given y[0] and A[t] (a sparse matrix ...
 

smoha...@gmail.com

unread,
Oct 31, 2014, 1:57:53 AM10/31/14
to sage-s...@googlegroups.com
 Here is a complete code which works the same as original code, independently:

L=2;

var('s','t','T')
DH=[2,0,0,2]
HS={(0,1):(-1/2)*(1-s), (0,2):(-1/2)*(1-s), (1,0):(-1/2)*(1-s), (1,3):(-1/2)*(1-s), (2,1):(-1/2)*(1-s), (2,4):(-1/2)*(1-s), (3,1):(-1/2)*(1-s), (3,2):(-1/2)*(1-s)};
HS1={(i,i): DH[i]*s+(L/2)*(1-s) for i in range(2^L)};
HS.update(HS1)

A=matrix(HS);
print A.str();

T=1;

f=A(s=t/T)
#f=A(s=0)


reset('i');
reset('x');

def Sch(t,y):
    return [ -i*sum(f[j,k]*y[j] for k in range(2^L)) for j in range(2^L)]  

NL1=[1 for i in range(2^L)];
TS=ode_solver()
TS.algorithm = "rk4"
TS.function=Sch
TS.y_0 = NL1
TS.ode_solve(t_span=[0,T], num_points=1000)
print TS.solution;


it results in:

[      s + 1 1/2*s - 1/2 1/2*s - 1/2           0           0]
[1/2*s - 1/2      -s + 1           0 1/2*s - 1/2           0]
[          0 1/2*s - 1/2      -s + 1           0 1/2*s - 1/2]
[          0 1/2*s - 1/2 1/2*s - 1/2       s + 1           0]
Traceback (click to the left of this block for traceback)
...
ValueError: error solving

but when I uncomment line 15: f=A(s=0), it results in:

       
WARNING: Output truncated!  
full_output.txt


[      s + 1 1/2*s - 1/2 1/2*s - 1/2           0           0]
[1/2*s - 1/2      -s + 1           0 1/2*s - 1/2           0]
[          0 1/2*s - 1/2      -s + 1           0 1/2*s - 1/2]
[          0 1/2*s - 1/2 1/2*s - 1/2       s + 1           0]
[(0, [1, 1, 1, 1]), (0.001, [1.0, 1.0, 0.9985011244377109, 1.0]),
(0.002, [1.0, 1.0, 0.997004495503373, 1.0]), (0.003, [1.0, 1.0,
0.9955101098295707, 1.0]), (0.004, [1.0, 1.0, 0.9940179640539353, 1.0]),
(0.005, [1.0, 1.0, 0.9925280548191384, 1.0]), (0.006, [1.0, 1.0,
0.9910403787728836, 1.0]), (0.007, [1.0, 1.0, 0.9895549325678991, 1.0]),
(0.008, [1.0, 1.0, 0.9880717128619303, 1.0]), (0.009000000000000001,
[1.0, 1.0, 0.9865907163177324, 1.0]), (0.010000000000000002, [1.0, 1.0,
0.9851119396030625, 1.0]),
...
upto:
(0.9970000000000008, [1.0, 1.0, 0.22413650845457392, 1.0]),
(0.9980000000000008, [1.0, 1.0, 0.22380055571943458, 1.0]),
(0.9990000000000008, [1.0, 1.0, 0.22346510653564, 1.0]),
(1.0000000000000007, [1.0, 1.0, 0.22313016014842943, 1.0])]

Sorry if it becomes late, I need time to switch to Linux to provide the code exactly as is.
Thanks for your attention.

Nils Bruin

unread,
Oct 31, 2014, 3:44:02 AM10/31/14
to sage-s...@googlegroups.com
On Thursday, October 30, 2014 10:57:53 PM UTC-7, smoha...@gmail.com wrote:
def Sch(t,y):
    return [ -i*sum(f[j,k]*y[j] for k in range(2^L)) for j in range(2^L)]  
Your error is here

sage: Sch(1,[1,2,3,4])
[-6*t, 0, 9/2*t - 9/2, -24*t]

as you can see, you're returning a vector that has a symbolic t in it. GSL doesn't like that and I suppose it's not accoring to GSL's specification.

You should be substituting t for the input value:

    def Sch(t0,y):
        return [ (-i*sum(f[j,k]*y[j] for k in range(2^L)))(t=t0) for j in range(2^L)] 

does the trick (at least no error arises). It's not strictly necessary to write t0 instead of t, but it makes for slightly less confusing code.

Reply all
Reply to author
Forward
0 new messages