Ask Your Question

florin's profile - activity

2019-06-26 10:47:45 -0500 received badge  Popular Question (source)
2019-01-31 05:05:30 -0500 asked a question how to get hints on fixing silly syntax errors

Hi, I just spent half an hour on these two commands.

def mov(): 
        P=matrix(QQ,[[1, 1],[0, 1]]);
        show(P)

I retyped them, I moved them in different cells, nothing, I get syntax error.

"<ipython-input-76-049764d5c40b>", line 1
    def mov() :
       ^
SyntaxError: invalid syntax

It works as long as I do not try to make a function

2018-12-17 02:18:25 -0500 answered a question Can I alias a sage command?

Sorry, I'm missing some syntax. I tried

def LP():
    alias 'LP'='MixedIntegerLinearProgram'
    load("LP.sage")

gets the error

File "<ipython-input-4-fc93b025772b>", line 2
    alias 'LP'='MixedIntegerLinearProgram'
             ^
SyntaxError: invalid syntax
2018-05-18 09:25:23 -0500 asked a question Differences between series and taylor

Hi. This morning I had two similar codes that worked:

var('s')
L=(1-exp(-s))/(s)
Ls=L.simplify_full()
show(parent(Ls))
tay=Ls.series(s,2*n+1) #or tay=taylor(Ls,s,0,2*n+1)?

and

    var('s')
    L=(1-exp(-s))/(s)
    Ls=L.simplify_full()
    show(parent(Ls))
    tay=taylor(Ls,s,0,2*n+1)

I couldn't see any difference in the parents, but with series I could continue getting Pade approx:

ta=tay.power_series(QQbar)
show(parent(ta))
n=2;Lp=ta.pade(n-1,n)

and with taylor I couldn't, hence my first question. Now, not even the first part, getting the series, does not work anymore, which adds a second question. It is true that the code that works was more complicated (including extra manipulations :the Pollaczek-Khinchine formula)

var('s ');rho=2/3;
#Compute Pollackek-Khinchine formula L_rui for the Laplace transf of ruin prob
R.<s> = PolynomialRing(QQbar) #algebraic numbers over Q, (or RR,CC)
FF = R.fraction_field()
L_f=(1-exp(-s))/(s);L_F=(1-L_f)/s
#Compute Pollackek-Khinchine formula L_rui for the Laplace transf of ruin prob
m1=limit(L_F,s=0) #the s is removable singularity
fe=L_F/m1
Fe=(1-fe)/s
L_rui=rho*Fe/(1-rho*fe) #in SR
Ls=L_rui.simplify_full()
n=2;
show(taylor(Ls,s,0, 2*n+1))
tay=Ls.series(s,2*n+1)
show(parent(tay))
ta=tay.power_series(QQbar)
show(parent(ta))
Lp=ta.pade(n-1,n)
wh,dec=Lp.partial_fraction_decomposition()
dec

Clearly, when simplifying for the purpose of asking on this forum, I added some silly mistake, but I can't see which

2018-05-15 03:55:02 -0500 answered a question Is it possible to change Polynomial Ring in the middle of a computation?

The working code for n=3 is

A=[1/6,2/6,3/6];ex=[2,6,4];rho=5/8;var('x s u')
F=sum(A[i]*exp(-x*ex[i]) for i in [0..2])#sum of exponentials
R.<s> = PolynomialRing(QQbar)
FF = R.fraction_field()
L_F=sum(A[i] /(s+ex[i]) for i in [0..2]);#Laplace transform of F
#Compute Pollackek-Khinchine formula L_rui for the Laplace transform of ruin probability
m1=L_F(s=0)
fe=L_F/m1
Fe=(1-fe)/s 
show(Fe) #note the s is simplified when working in QQbar
L_rui=rho*Fe/(1-rho*fe)
R.change_ring(RR)
FF = R.fraction_field()
wh,LL=L_rui.partial_fraction_decomposition()
show(LL[0])

with output

[0.5332274783298694?s+1.087668109036214?,0.07804278965136865?s+2.967947295921641?,0.01372973201876195?s+5.575963542410567?]

I need now to "remove" somehow the ? before taking

inverse_laplace(LL[0],s,u)

It would be nice if something like

inverse_laplace(LL[0].change_ring(RR),s,u)

or inverse_laplace(RR(LL[0]),s,u) would work

2018-05-14 12:03:38 -0500 asked a question Is it possible to change Polynomial Ring in the middle of a computation?

Hi, I'm trying to invert the "Pollaczek-Khinchine" Laplace transform when it is rational

This works for me at degree 2:

var('x,s')
Fx = (1/6*exp(-2*x)+5/6*exp(-6*x));rho=2/3
print('Hyperexponential claims:',Fx)
R.<s> = PolynomialRing(QQbar)#when all coefficients are not integer, use CC
FF = R.fraction_field()
L_F=laplace(Fx,x,s)#Laplace transform of F
#Compute Pollackek-Khinchine (PK) formula L_rui for the Laplace transform (LT) of ruin probability
m1=L_F(s=0)
fe=L_F/m1
Fe=(1-fe)/s
L_rui=rho*Fe/(1-rho*fe)
show(L_rui.simplify_full())
inverse_laplace(SR(L_rui),s,u)

but not at degree 3, since I do not know how to use partial_fraction_decomposition, and then to switch to RR numbers and then invert . If I start in R. = PolynomialRing(RR), for an already known LT, everything is fine. But, a certain simplification by s in PK formula will become impossible due to rounding errors, so I am forced to start with R. = PolynomialRing(QQbar) After obtaining the partial_fraction_decomposition, I must apply RR to all numbers , but I do not manage to do it. Without that conversion, inverse_laplace won't work

2018-05-14 11:07:14 -0500 commented answer inverse_laplace of a fraction whose denominator has real roots (or complex)

OK I got it, I just need to replace QQbar by RR. Silly me !!!!

2018-05-14 10:59:50 -0500 commented answer inverse_laplace of a fraction whose denominator has real roots (or complex)

the conversion is doable by RR(). Is there an easy way to apply RR to all parts of an expression?

2018-05-14 10:35:58 -0500 commented answer inverse_laplace of a fraction whose denominator has real roots (or complex)

The Pade of the uniform program is

var('s'); rho=2/3;L_f=(1-exp(-s))/(s) #LT of uniform density
L_F=(1-L_f)/s #LT of uniform survival function
#Compute Pollackek-Khinchine formula L_rui for the Laplace transform of ruin probability
m1=limit(L_F,s=0) #the s is removable singularity
fe=L_F/m1
Fe=(1-fe)/s 
L_rui=rho*Fe/(1-rho*fe)
show(L_rui)
n=2;
tay=taylor(L_rui,s,0,2*n+1)
ta=tay.power_series(QQ)
Lp=ta.pade(1,2)
Lp

with answer

(5/8*s + 15/2)/(s^2 + 45/4*s + 45/4)

it only remains to parfrac this. Thanks, Florin

2018-05-14 04:10:44 -0500 answered a question inverse_laplace of a fraction whose denominator has real roots (or complex)

in competitive risks models, there are more interesting things to do OK, I'm dealing with a risk expert :) which University are you based in?

I am just trying now to program in Sage a pedagogic exercise: compute the ruin probability for the Cramer-Lundberg model with a) hyperexponential claims; this should be easy, except that I am novice enough to Sage to have forgotten how these 1.33456? numbers are called, and how to convert an expression involving many of these numbers to a type which will be accepted by the command inverse_laplace

b) uniform claims (so the LT is not rational); after rationalizing the LT by Pade, this reduces to previous; it's a one liner in Mathematica, but takes hours in sage due to the crude help system.

2018-05-14 04:04:41 -0500 asked a question inverse_laplace of a fraction whose denominator has real roots (or complex)

Hi, this is the simplest and very important method of Laplace inversion . It can be applied to any Laplace transform (LT), by starting with a Pade approximation of the LT, then partial fractions and inversion. For some reason, a program I had written last year stopped working

def Ruin(Fx, rho, x, u):#assumes rational survival function Fx, i.e. a hyperexponential density
    var('s')
    L_F=laplace(Fx,x,s) 
    m1=L_F(s=0) #some algebraic manipulations known as Pollaczek-Khinchine formula
    fe=L_F/m1
    Fe=factor((1-fe)/s)
    L_rui=rho*Fe/(1-rho*fe)
    C=ComplexField(53);
    dec=Frac(C['s'])(L_rui).partial_fraction_decomposition();
    n=len(dec)
    par=[inverse_laplace(dec[1][i],s,u) for i in [0..n]];
    rui=sum(par)
    return rui,  L_rui, fe

It's pretty easy to repair the program when roots are rational -- see question "partial_fraction_decomposition" with possibly "complex roots", again. For nonrational roots, I proposed there the following test case

var('s,u')
R.<s> = PolynomialRing(QQbar) 
F = R.fraction_field()
L=3/4*(19*s^2 + 156*s + 284)/(19*s^3 + 174*s^2 + 422*s + 228)
whole,LL=L.partial_fraction_decomposition()
show(LL[0])
inverse_laplace(LL[0],s,u)

Thanks in advance :)

2018-05-13 14:04:35 -0500 answered a question "partial_fraction_decomposition" with possibly "complex roots", again

Samuel and Emanuel, thanks for teaching me the magic of QQbar and SR! one more question, please. How to do this inverse_laplace of a fraction whose denominator roots may be real (and maybe even complex)? For example,

var('s,u')
R.<s> = PolynomialRing(QQbar) 
F = R.fraction_field()
L=3/4*(19*s^2 + 156*s + 284)/(19*s^3 + 174*s^2 + 422*s + 228)
whole,LL=L.partial_fraction_decomposition()
show(LL)

Here I'm stuck with the "decimals?" numbers and

inverse_laplace(L,s,u)

ends again in

TypeError: unable to convert [0.6934316866155686?/(s + 0.7570751092241817?), 0.0477251478733486?/(s + 2.861392366086696?), 0.00884316551108293?/(s + 5.539427261531228?)] to a symbolic expression

on the other hand

SR(L).partial_fraction()

fails to find the real roots, and inverse_laplace fails also. Thanks, F

The problem is really how to do partial_fractions and then extract the answer, but I added more of the story to make it clear this is a numeric question. For a first answer, I do not need 100 digits precision :)

I have added a new question "inverse_laplace of a fraction whose denominator roots are real (or complex) to relance this with more details

2018-05-06 04:46:25 -0500 answered a question "partial_fraction_decomposition" with possibly "complex roots", again

Thanks, Samuel :) Starting with the declaration works better :) It's still a bit unfortunate though that the first coefficient 4/7 is not obtained as fraction. I need then an inverse_laplace and this seems to work only for second component

var('s,u')
R.<s> = PolynomialRing(QQbar)
F = R.fraction_field()
L = 2*(s + 3)/(3*s^2 + 13*s + 10)
whole,LL=L.partial_fraction_decomposition()
show(LL)
inverse_laplace(LL[1],s,u)

It would be nice if there was a simple way to convert all the ?numbers in my list of fractions to closest fractions (apply nearby_rational? to all parts of an expression)

2018-04-30 12:10:04 -0500 received badge  Nice Question (source)
2018-04-30 03:58:31 -0500 asked a question "partial_fraction_decomposition" with possibly "complex roots", again

Hi I come back to this question, even though it's been answered before, since I am still not able to make it work. I should mention maybe that I am just trying to teach undergraduate students to invert rational Laplace transforms (for myself I am able to afford a Mathematica licence, but it would be nice to be able to show students that such simple things may be done nowadays for free). Following a previous answer, I tried

L = 2*(s + 3)/(3*s^2 + 13*s + 10)
Ks=FractionField(PolynomialRing(CC,names='s'))
Lr=Ks(L)

Of course, with quadratic rational roots, I could do this by hand, but the purpose here is to do it when you do not know the roots. The first two commands work, but the third has error message

TypeError: ('cannot convert {!r}/{!r} to an element of {}', 2*(s + 3)/(3*s^2 + 13*s + 10), 1.00000000000000, Fraction Field of Univariate Polynomial Ring in s over Complex Field with 53 bits of precision)

I should add that a different attempt which used to work last year

C=ComplexField(53);
dec=Frac(C['s'])(Lrs).partial_fraction_decomposition();

gets now same error message. So, this is probably due to an "improvement" of sage

2018-04-18 13:27:33 -0500 commented answer "partial_fraction_decomposition" with "complex roots"

Dan, thanks for the thorough answer !!! I only need to invert Laplace transforms with complex poles, so I tried

L = 2*(s + 3)/(3*s^2 + 13*s + 9)
Ks=FractionField(PolynomialRing(CC,names='s'))
Lr=Ks(L)

Unfortunately, the third command gets an error ?

TypeError: ('cannot convert {!r}/{!r} to an element of {}', 2*(s + 3)/(3*s^2 + 13*s + 9), 1.00000000000000, Fraction Field of Univariate Polynomial Ring in s over Complex Field with 53 bits of precision)
2018-04-18 13:13:20 -0500 commented answer "partial_fraction_decomposition" with "complex roots"

Thanks for the thorough answer, Dan !!! In that spirit (I just need to invert Laplace transforms with complex roots), I tried

L = 2*(s + 3)/(3*s^2 + 13*s + 9)
Ks=FractionField(PolynomialRing(CC,names='s'))
Lr=Ks(L)

The first two commands work, but the third has error message

TypeError: ('cannot convert {!r}/{!r} to an element of {}', 2*(s + 3)/(3*s^2 + 13*s + 9), 1.00000000000000, Fraction Field of Univariate Polynomial Ring in s over Complex Field with 53 bits of precision)
2018-04-06 14:42:27 -0500 asked a question "partial_fraction_decomposition" with "complex roots"

Hi six months ago I used a hint from Zimmerman's book

var('s')

L=2(s + 3)/(3s^2 + 13*s + 10)

C=ComplexField(53)

dec=Frac(C['s'])(L).partial_fraction_decomposition()

but this does not work anymore (see below). How to do this? Thanks

----> 2 dec=Frac(C['s'])(Lrs).partial_fraction_decomposition();

TypeError: ('cannot convert {!r}/{!r} to an element of {}', 2(s + 3)/(3s^2 + 13*s + 10), 1.00000000000000, Fraction Field of Univariate Polynomial Ring in s over Complex Field with 53 bits of precision)

/opt/sagemath-8.1/src/sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9641)() 937 if mor is not None: 938 if no_extra_args: --> 939 return mor._call_(x) 940 else: 941 return mor._call_with_args(x, args, kwds)

/opt/sagemath-8.1/src/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4928)() 152 print(type(C), C) 153 print(type(C._element_constructor), C._element_constructor) --> 154 raise 155 156 cpdef Element _call_with_args(self, x, args=(), kwds={}):

/opt/sagemath-8.1/src/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4796)() 147 cdef Parent C = self._codomain 148 try: --> 149 return C._element_constructor(x) 150 except Exception: 151 if print_warnings:

/opt/sagemath-8.1/local/lib/python2.7/site-packages/sage/rings/fraction_field.py in _element_constructor_(self, x, y, coerce) 616 except AttributeError: 617 raise TypeError("cannot convert {!r}/{!r} to an element of {}", --> 618 x0, y0, self) 619 try: 620 return self._element_class(self, x, y, coerce=coerce)

2018-03-10 00:33:45 -0500 received badge  Enthusiast
2018-03-08 06:07:23 -0500 answered a question how to find the syntax error

The indentation was fine, I just didn't know how to preserve it in the question by copy paste I indented the code with syntax error by hand to show you (but this still doesn't show in preview),
I recopied it into Sage, it works now. I'll go by a gun

i=3 while i>0 : print i, 'looping'
i=i-1

2018-03-08 05:58:58 -0500 asked a question how to find the syntax error

Hi

Here's a short code (copy-paste) that gets me syntax error i=3 while i>0 : print i, 'looping'    i=i-1

"<ipython-input-7-8e7eba3f21a0>", line 4 i=i-Integer(1)

I retyped it a second time; now it works, but I don't see any difference. i=3 while i>0 : print i, 'looping' i=i-1

Retyping only the line 4 with the error won't help, retyping the previous lines or restarting the kernel won't help either. It's like a phantom error

2017-12-02 04:16:35 -0500 asked a question can series make mistakes?

series seems to make errors . luckily, taylor works correctly. start with L_F=(exp(-s)-1+s)/(s^2/2) (laplace transform of the "equilibrium" uniform density) and switch to Pollaczek laplace transform L_F /(1- epsilon * L_F);
You can check that the moments expansions t, t1 differ

var('s');n=2 L_F=(exp(-s)-1+s)/(s^2/2) #satisfies L_F(s=0)=1 L_L=L_F/(1-L_F/3) t = L_L.series(s,2n+2) t1= taylor(L_L,s,0,2n+2) print t1

2017-12-02 03:58:56 -0500 received badge  Editor
2017-12-02 03:08:41 -0500 answered a question using pade approx

it works now! Thanks both of you and excuses for my silly novice questions. Both series and taylor which I had tried before work fine (I guess both must have advantages sometimes if they are kept ?)

adding the QQ magic changed the donna Here's the example var('s') L_F=(exp(-s)-1+s)/(s^2)
t=L_F.series(s,6) #t = taylor(L_F,s,0,6) h=QQ['s'] h.pade(2,2)

2017-12-01 04:13:14 -0500 asked a question using pade approx

Hi 1) From the only example I found s = PowerSeriesRing(QQ,'s').gen() a=exp(s);a.pade(4, 0) type(a)

it seems this works for type 'sage.rings.power_series_poly.PowerSeries_poly'

But I have a type 'sage.symbolic.expression.Expression' b, and was unable to convert b to the type required to apply pade

2) There's an alternative rational.reconstruct , but that seems to be geared to computations modulo (n) 3) In conclusion, it seems at current stage the simplest is to write one's own Pade? Thanks, Florin

2017-11-27 01:39:32 -0500 received badge  Scholar (source)
2017-11-26 06:44:53 -0500 answered a question using Jupiter notebooks .ipynb created on Windows (Oracle VM virtual box) on sage, linux

Merci, Samuel :)

2017-11-24 14:08:25 -0500 asked a question using Jupiter notebooks .ipynb created on Windows (Oracle VM virtual box) on sage, linux

Hi

1) I use Jupiter notebooks .ipynb created on Windows (Oracle VM virtual box) at home, and would like to upload and use them on our linux sage installation, in our University

It seems this is impossible (upload of .ipynb is refused by linux sage)

2) As a substitute, I noticed on a notebook .sage created by someone else that it is at least pure text, and hence can be copied-pasted on a new file in the linux machine. Is it possible to convert my .ipynb notebooks to .sage notebooks? This would allow using them both as notebooks and as a source of copypaste when upload is refused. In fact, can .sage files be created on Windows (Oracle VM virtual box)?

Thanks, Florin

2017-10-21 05:45:03 -0500 received badge  Student (source)
2017-10-21 05:44:11 -0500 answered a question Converting ipynb to latex in Jupyter: is it ?nbconvert /home/sage/Documents/PKfactor.ipynb --to latex

error message was SyntaxError: invalid syntax

2017-10-21 05:44:11 -0500 asked a question Converting ipynb to latex in Jupyter: is it ?nbconvert /home/sage/Documents/PKfactor.ipynb --to latex

I tried nbconvert /home/sage/Documents/PKfactor.ipynb --to latex (on Windows, virtual machine Sage 8.0), but it didn't work