Ask Your Question

Emmanuel Charpentier's profile - activity

2019-02-15 00:45:50 -0600 answered a question Conditional Plot, plot f(x+iy) if g(x+iy) satisfies some kind of condition

No elaborate solution (some knowledge of f and g would come handy). A couple of hints :

  • The syntax of a conditional expression in Sage is : result-if true if condition else result-if-false. No comma involved.

  • implicit_plot3d? might make your day (see the region argument)... but is damn heavy !

  • In the current implementations of plot3d, friends and cousins, points having value float('nan') do not plot. This might come handy but is not documented and might go without warning at the next revision of those functions.

2019-02-13 11:04:55 -0600 answered a question exponential equation solve problem

Well...

sage: b.solve(x)[0].log().log_expand().solve(x)
[x == 1/2*(log(3) + 4*log(2) - log(4/5))/(log(3) - log(2))]

<Swing>"Who could ask for anything more ?"</Swing>

Maybe looking for other solutions ? (Hint, hint...)

EDIT : Further hint : the logarithm is a "multivalued function" in the complex field (i. e. not a function strictly speaking). Any time you take a log, you introduce further, possibly spurious, solutions...

EDIT 2: Full solution, since no one seemed to see the problem :

Original problem:

,----
| b=4*3^(2*x-1)==5*4^(x+2)
`----

if the members of this equations are equal, so do their logs. So we might try to solve :

,----
| Lb=b.log().expand_log()
| Lb
`----

(2*x - 1)*log(3) + 2*log(2) == 2*(x + 2)*log(2) + log(5)

But the converse is not true !. More specifically :

,----
| z, z_1, z_2=var("z, z_1, z_2", domain="integer")
| (e^(x+2*I*pi*z)).maxima_methods().exponentialize()
`----

e^x

Therefore, we have to consider the solutions of :

,----
| Lb2=(Lb.lhs()+2*I*pi*z_1==Lb.rhs()+2*I*pi*z_2)
| Lb2
`----

2*I*pi*z_1 + (2*x - 1)*log(3) + 2*log(2) == 2*I*pi*z_2 + 2*(x + 2)*log(2) + log(5)

for any integer values of z_1 and z_2. The solutions are :

,----
| Sol=Lb2.solve(x, to_poly_solve=True)
| Sol
`----

[x == 1/2*(-2*I*pi*z_1 + 2*I*pi*z_2 + log(5) + log(3) + 2*log(2))/(log(3) - log(2))]

i. e. $$\left[x = \frac{-2 i \pi z_{1} + 2 i \pi z_{2} + \log\left(5\right) + \log\left(3\right) + 2 \log\left(2\right)}{2 {\left(\log\left(3\right) - \log\left(2\right)\right)}}\right]$$

which is unique for any difference $z=z_1-z_2$.

Checking these solutions is not as direct as one could wish. But one can check that the ratio of the two members is one :

,----
| (b.rhs()/b.lhs()).subs(Sol).log().log_expand().expand().factor().exp()
`----

1

One can note that the non-real roots of this equation are somehow missed by Sage (and Maxima). This is also true for giac and sympy. But Mathematica returns them:

,----
| mathematica.Reduce(b,x)
`----

Element[C[1], Integers] && x == -((2*I)*Pi*C[1] + 2*Log[2] + Log[3] + Log[5])/ (2*(Log[2] - Log[3]))

i. e. $$c_1\in \mathbb{Z}\land x=-\frac{2 i \pi c_1+\log (5)+\log (3)+2 \log (2)}{2 (\log (2)-\log (3))}$$

HTH,

2019-02-09 04:09:53 -0600 answered a question Is there an easy way to get the matrix of coefficients from a product of a matrix and a vector?

I beg to differ with the commenters.

Staying at high-school level, your system boils down to a system of three linear equations with nine unknowns. It has therefore a sextuple infinity of solutions. And sage knows that :

A=[]
B=[]
X=[]
for u in (1..3):    
    X.append(var("x_{}".format(u), latex_name="x_{{{}}}".format(u)))
    B.append(var("b_{}".format(u), latex_name="x_{{{}}}".format(u)))
    for v in (1..3):
        A.append(var("a_{}_{}".format(u,v),
                     latex_name="a_{{{},{}}}".format(u,v)))
A=matrix(entries=A,nrows=3)
B=vector(B)
X=vector(X)
S=[B[u]==(A*X)[u] for u in range(3)]
Sol=solve(S,[u for u in A.variables()])
Sol
[[a_1_1 == -(r24*x_2 + r21*x_3 - b_1)/x_1, a_1_2 == r24, a_1_3 == r21, a_2_1 == -(r23*x_2 + r22*x_3 - b_2)/x_1, a_2_2 == r23, a_2_3 == r22, a_3_1 == -(r20*x_2 + r19*x_3 - b_3)/x_1, a_3_2 == r20, a_3_3 == r19]]

Or, more clearly, the only solution is:

$${a_{1,1}} = -\frac{r_{24} {x_{2}} + r_{21} {x_{3}} - {x_{1}}}{{x_{1}}}$$', '$${a_{1,2}} = r_{24}$$', '$${a_{1,3}} = r_{21}$$', '$${a_{2,1}} = -\frac{r_{23} {x_{2}} + r_{22} {x_{3}} - {x_{2}}}{{x_{1}}}$$', '$${a_{2,2}} = r_{23}$$', '$${a_{2,3}} = r_{22}$$', '$${a_{3,1}} = -\frac{r_{20} {x_{2}} + r_{19} {x_{3}} - {x_{3}}}{{x_{1}}}$$', '$${a_{3,2}} = r_{20}$$', '$${a_{3,3}} = r_{19}$$'

In other words, your solution is a vector space of dimension six. This means that you could pick six of the elements of your and sole for the three last. For example:

solve(S,[a_1_1, a_2_2, a_3_3])
[[a_1_1 == -(a_1_2*x_2 + a_1_3*x_3 - b_1)/x_1, a_2_2 == -(a_2_1*x_1 + a_2_3*x_3 - b_2)/x_2, a_3_3 == -(a_3_1*x_1 + a_3_2*x_2 - b_3)/x_3]]

'$${a_{1,1}} = -\frac{{a_{1,2}} {x_{2}} + {a_{1,3}} {x_{3}} - {x_{1}}}{{x_{1}}}$$', '$${a_{2,2}} = -\frac{{a_{2,1}} {x_{1}} + {a_{2,3}} {x_{3}} - {x_{2}}}{{x_{2}}}$$', '$${a_{3,3}} = -\frac{{a_{3,1}} {x_{1}} + {a_{3,2}} {x_{2}} - {x_{3}}}{{x_{3}}}$$'

Sage has other, higher level, methods for solving this kind of linear algebra problems, whose discovery is left to the reader as an exercise ;-).

2019-02-07 01:24:48 -0600 received badge  Nice Answer (source)
2019-02-05 16:15:48 -0600 answered a question How can I avoit the scientific notation on the y-axis?

Well...

sage: e^(2000*0.017).n()
5.83461742527455e14

Do yoy really need 14-digits wide y-axis markers ?

2019-02-05 04:00:34 -0600 answered a question Problem with Padé approximation

Well...

sage: S=cos(x).taylor(x,0,10).power_series(QQ).pade(2,4);S
(-244/3*x^2 + 200)/(x^4 + 56/3*x^2 + 200)
sage: M=mathematica.PadeApproximant(cos(x),[x,0,[2,4]]).sage();M
-4*(61*x^2 - 150)/(3*x^4 + 56*x^2 + 600)
sage: bool(M==S)
True

By the way, the Padé approximation is slightly less precise than the Taylor development of the equivalent degree:

sage: S(x=pi).n()
-1.25140289236071
sage: cos(x).taylor(x,0,6)(x=pi).n()
-1.21135284298250

And FredericC's solution is equivalent:

sage: t=QQ[['t']].0
sage: T=t.cos().pade(2,4);T
(-244/3*t^2 + 200)/(t^4 + 56/3*t^2 + 200)
sage: bool(T(x)==S)
True

Your Sage version (which is antique, by Sage standards...) gives you bullshit.

2019-02-03 07:24:53 -0600 answered a question Piecewise in SageTeX

Somebody called gh-JCGoran seems to have stumbled on this already, and reported Trac#27078.

But the implementation isn't trivial.

I also note that the sympy representatin of this function is questionable...

2019-01-31 20:53:47 -0600 received badge  Nice Answer (source)
2019-01-31 09:11:41 -0600 answered a question why gives a wrong result?

Problem similar to this previous question, which generated the bug report Trac#26563.

BTW, you should try to create questions a bit less err... elliptic (pun not intended but kept...). Your question is more a riddle than a question...

2019-01-27 12:01:07 -0600 answered a question Bug in series expansion?

What about :

sage: var("q")
q
sage: f(q)=(q+1)/(q-3);f
q |--> (q + 1)/(q - 3)
sage: f.maxima_methods().powerseries(q,oo)
4*sum(3^(i1 - 1)/q^i1, i1, 0, +Infinity) - 1/3
2019-01-23 13:00:53 -0600 received badge  Nice Answer (source)
2019-01-21 10:52:15 -0600 answered a question SageMath export to latex Document

Foreword :

  • Basic LaTeX literacy is a must for almost any "hard science" curriculum at one point or another. Gaining this ability as soon and as painlessly as possible is a plus for your students. Since many (most ?) tools/markup languages offer the possibility of using LaTeX markup for various tasks, a basic LaTeX knowledge (structure, maths markup, cross-references) is useful "almost everywhere".

  • In 40 years, solution for a myriad of document preparation problems have been developed for LaTeX (mostly in view of paper printing). Using LaTeX allows you (and your students) to tap this resource...

That said, LaTeX care and feeding may be considered superfluous at the beginning of a curriculum. A few possibility come to mind :

  • Pythontex can be used to mix text and formal computation, not only in sage but also in Python and a few other languages, à la LaTeX+Sagetex. But the markup language is still LaTeX. I know tha Geoffer Poore, pythontex's author, is planning a similar tool for markdown-based tdocuments, but I haven't the foggiest notion of the advancement of this project...

  • rmarkdown ( and knitr) are a tool giving the ability to mix R code and LaTeX or markdown text ; Markdown is lighter (and less powerful) than LaTeX. and the reticulate package, which allows to use Python source blocks, may be coaxed to accept Sage blocks (note : this is not (yet) implemended...).

Another possibility is to use sage blocks on org mode's Babel. Org mode`'s markup is less verbose and may be considered by some as lighter than LaTeX (although less powerful and less consistent), possibly easier to use.

Its largest drawback (and, possibly, advantage) is that it is (currently) Emacs-bound (although ports to other editors have been started, I'm not sure that Org-mode can be effectively compiled by anything else than emacs) : using it entails the Matterhorn-like learning curve (and benefits) of Emacs...

But the real points to Babel are :

  • the ability to use more than one language for code blocks. For example, sage or Mathematica for formal computation, R, Matlab or octave for statistics and numerical computation, graphviz for graphs, etc... About 70 languages are currently (more or less) supported by Babel.

  • The ability to get output as LaTeX/PDF, but also HTML, Beamer slides, HTML slides, LibreOffice's .odt and even Word's .docx. Quite useful when working in low-computer-literacy environment (read "administration"). Add pandoc to the mix, and the possibilities are almost boundless...

So this may be an interesting tool for the instructor, but probably less so for the students.

Note that, in any case, your students will have to use a markup language to be able to do about anything ; learning to leave the damned mouse alone (i. e. renounce WYSIWYG) is probably the hardest step of the learning process. Th exact markup language is probably not too important : markdown is less verbose than LaTeX, but has less possibilities (and, as far as I know, is not Turing-complete...).

2019-01-19 13:31:36 -0600 commented question Problem with boring message

I'm running H. sapiens sapiens L., without any upgrades such as divination coprocessor or telepathic interface. Without any details about what you attempting, it's quite hard for me to guess if LaTeX is or not needed for it.

What in hell are you trying to do ?

2019-01-18 13:03:55 -0600 received badge  Nice Answer (source)
2019-01-18 01:10:01 -0600 answered a question Recovering numbers from continued fraction

What's wrong with this :

sage: continued_fraction([1]*12).n()
1.61805555555556
sage: continued_fraction([1]*12)._rational_()
233/144
sage: continued_fraction([1]*12)._rational_().n()
1.61805555555556
2019-01-17 15:18:49 -0600 answered a question Multiplying Roots of a Polynomial

Let's see...

sage: f=x^2*((x+1/x)^2-1)

The roots are :

sage: f.roots(multiplicities=False)
[-sqrt(1/2*I*sqrt(3) - 1/2),
 sqrt(1/2*I*sqrt(3) - 1/2),
 -sqrt(-1/2*I*sqrt(3) - 1/2),
 sqrt(-1/2*I*sqrt(3) - 1/2)]

Therefore, the factorized polynom is :

sage: prod([x-u for u in f.roots(multiplicities=False)])
(x + sqrt(1/2*I*sqrt(3) - 1/2))*(x - sqrt(1/2*I*sqrt(3) - 1/2))*(x + sqrt(-1/2*I*sqrt(3) - 1/2))*(x - sqrt(-1/2*I*sqrt(3) - 1/2))

And the roots' products is:

sage: prod(f.roots(multiplicities=False)).expand()
1

Whereas moving to a polynomial ring, more specialized, is often useful, it is not necessary here...

2019-01-17 14:21:32 -0600 answered a question How can I solve the following (linear) differential equation?

[ Not really an answer, but refinements needing more than the 500 characters of a comment ]

calc314's answer is an explicit solution :

import sympy
y=function("y")(x)
E1=diff(y(x),x,3)(x)-3*diff(y(x),x,2)(x)+diff(y(x),x)(x)-5*y(x)==0
Sol=sympy.dsolve(*map(sympy.sympify, [E1,y(x)]))._sage_()
Sol
y(x) == C3*e^(x*complex_root_of(x^3 - 3*x^2 + x - 5, 2)) + C2*e^(x*complex_root_of(x^3 - 3*x^2 + x - 5, 1)) + C1*e^(x*complex_root_of(x^3 - 3*x^2 + x - 5, 0))

The function complex_root_of(P,i) is a formal function representing the ith root of the polynomial P (in a stable fashion), which can be evaluated numerically). See its help docstring, as well as sympy's CRoot's.

If symbolic solutions are needed, you can also extract the characteristic polynomial and solve it symbolically :

Sol.rhs().operands()[0].operands()[1].operands()[0].operands()[1].operands()[0].solve(x)
[x == -1/2*(1/9*sqrt(235)*sqrt(3) + 3)^(1/3)*(I*sqrt(3) + 1) + 1/3*(I*sqrt(3) - 1)/(1/9*sqrt(235)*sqrt(3) + 3)^(1/3) + 1,
 x == -1/2*(1/9*sqrt(235)*sqrt(3) + 3)^(1/3)*(-I*sqrt(3) + 1) + 1/3*(-I*sqrt(3) - 1)/(1/9*sqrt(235)*sqrt(3) + 3)^(1/3) + 1,
 x == (1/9*sqrt(235)*sqrt(3) + 3)^(1/3) + 2/3/(1/9*sqrt(235)*sqrt(3) + 3)^(1/3) + 1]

But I think that the "formal root" form s a better expression in a lot of situations.

2019-01-16 02:16:32 -0600 commented question Wrong solution/output for differential equation

giac("desolve([y'=4*y/x+x*sqrt(y),y(1)=1],y)").sage() should give you [1/4*(ln(x) + 2)^2*e^(4*ln(x))].

2019-01-15 01:42:01 -0600 commented question Wrong solution/output for differential equation
  1. The problem is specific to maxima :

    • sympy has another, distinct, failure.

    • fricas fails entirely.

    • giac gives an acceptable answer.

  2. You should follow the steps suggested in the documentation, possibly also reporting this bug against maxima.

2019-01-13 05:25:08 -0600 commented answer Combine plots with built-in Maxima, trajectory in Sage available?

I see what you mean : your initial condition set admits both -1 and 1 as roots for _C, but your differenial equation E1 is verified only for _C=1. Mathematica gives you both solutions without warning, and Maxima picks the wrong one.

Film at 11...

BTW : sympy complains of NotImplementedError: Initial conditions produced too many solutions for constants, and throws the towel. giac seems to pick the right one.

Workaround :

  • Solve the (system of) differential equation(s), getting a solution with symbolic constants (usually not declared : declare them to Sage...)
  • Build a system composed of the ICS and the original differential equation(s).
  • Solve this system for the symbolic constants.
2019-01-12 07:06:52 -0600 answered a question Combine plots with built-in Maxima, trajectory in Sage available?

[ Not really an answer, but the damn server software refuses this as a comment... ]

I don't understand your beef with desolve's answer :

sage: y=function("y")(x)
sage: E1=diff(y(x),x)==4*y(x)/x+x*sqrt(y(x))
sage: S1=desolve(E1,y(x));S1
1/4*(2*_C + log(x))^2*x^4
sage: var("_C")
_C
sage: bool(E1.subs(y(x)==S1).subs(diff(y(x),x)==diff(S1,x)).canonicalize_radical())
True

Therefore, S1 is a set of solutions of E1 (though possibly not the set of solutions of S1, but this is another question).

BTW, both mathematica and sympy give the same answers (modulo presentation and constant's name) :

sage: mathematica("Factor[Part[DSolve[D[y[x],x]==4*y[x]/x+x*Sqrt[y[x]],y[x],x],1,1]]")
y[x] -> (x^4*(2*C[1] + Log[x])^2)/4
sage: import sympy
sage: sympy.dsolve(*[sympy.sympify(u) for u in [E1,y(x)]])_sage_()
y(x) == 1/4*(2*C1 + log(x))^2*x^4

algorithm="fricas" can't (currently) solve this ODE. Open question : quid of giac ?

2019-01-08 01:18:02 -0600 commented question Sage Notebook IOPub data rate exceeded error?

You should check that the documentation your research has led you to is applicable to Sage's jupyter and jupyter modules' version : Sage is not up to date on ths respect (and won't until the port to Python 3 is complete : recent jupyter versions need Python 3).

2019-01-03 10:30:42 -0600 received badge  Good Answer (source)
2018-12-04 14:42:46 -0600 commented question Sage program for 40!+k project

Homework ?

2018-12-03 09:49:22 -0600 answered a question Sage doesn't simplify a fraction if it's multiplied by 2

Probable maxima idiosyncrasy :

sage: E=2*(1-cos(x))/sqrt(1-cos(x))
sage: E.simplify_full()
-2*(cos(x) - 1)/sqrt(-cos(x) + 1)
sage: (E/2).simplify_full()*2
2*sqrt(-cos(x) + 1)
sage: import sympy
sage: sympy.sympify(E).simplify()._sage_()
2*sqrt(-cos(x) + 1)
2018-12-03 09:21:10 -0600 answered a question How to get the collection of all functions from X to Y in SageMath

Disclaimer : I know zilch about combinatirics. This problem might have elegant (and already programmed) solutions among the combinatorics tools of Sage. However, the problem seems simple enough to attempt a "naïve" solution.

A function from $X$ to $Y$ is the set of tuples $[ (x_i,y_j),...,i=1,\dots,n,j\in[0..n]]$, meaning that $x_i$ has $y_j$ as image, the notation $(x_i,y_0)$ denoting the case where $x_i$ has no image. Let $S_{n,m}$ the set of all functions from $X$ to $Y$ when X has $n$ elements and Y has $m$ elements.

1): the set $S_{1,m}$ is simply $[(x_1,y_j),j=0,...,m]$.

2): knowing $S_{n,m}$, one can simply build $S_{n+1,m}$ by adding (i. e. concatenating) to each of the functions in $S_{n,m}$ a member of $[(x_{n+1},y_j),j=0,...,m]$. In other words, S_{n+1,m}$ is the cartesian product of $S_{n,m}$ and $[(x_{n+1},y_j),j=0,...,m]$.

3): 1) and 2) are sufficient to build $S_{n,m}$ by recurrence on n. It results immediately that the cardinal is $(m+1)^n$, including the degenerate case where no element of $X$ has an image in $Y$.

The implementation, whose details depend of the planned use of the functions, is, as usual, left to the reader as an exercise ;-).

2018-12-02 16:29:01 -0600 answered a question Variable Not Found while Plotting Finite Sum

plot is suited to the plotting of functions of a real variable, a priori defined in the plotting range.Your function is a function of a discrete variable (a suite), i. e. defined nowhere on the plotting range except of a set of null measure. For which plot_list is the "right" solution.

2018-11-30 02:40:27 -0600 received badge  Nice Answer (source)
2018-11-29 11:54:54 -0600 answered a question log base 2 in sagemath

What's wrong with :

sage: log(1000,2).n()
9.96578428466209

Note that this isn't an exact answer (the exact answer is log(1000)/log(2)). But it's a reasonable numerical approximation of a quantity whose "exact" numerical expression would be infinite...

Further discussion deserves reading part III of this excellent book as a prerequisite...

2018-11-24 13:26:11 -0600 commented answer finding solutions with exponential series expansion

Neither Fricas nor Giac can solve this symbolically :

sage: giac("solve(111*t/1000-(1-exp(-3*t/10)),t)")
list[-6.76523232004e-14,8.25101463236]
sage: fricas("solve(111*t/1000-(1-exp(-3*t/10)),t)")
[]

Mathematica gives an answer subsuming Sympy's and asserting the existence of an infinity of (complex) solutions :

sage: mathematica("Reduce[111*t/1000==1-Exp[-3*t/10],t]")
(Element[C[1], Integers] && 
  t == 1000/111 + (10*ProductLog[C[1], -100/(37*E^(100/37))])/3) || t == 0

(Reformatted from Mathematica's help) :

ProductLog][z]
gives the principal solution for w in z == w e^w. 
ProductLog[k,z]
gives the k^th solution.

From Sage's lambert_w? :

This function satisfies the equation

  z = W_n(z)  e^{W_n(z)}

Same difference...