[sympy] Re: problem trying to solve an equation

2023-03-06 Thread Chris Smith
An answer is given in https://github.com/sympy/sympy/issues/24824

On Sunday, March 5, 2023 at 1:26:50 PM UTC-6 toaff...@gmail.com wrote:

> I think you should   simplifythe equation and reduce the computational 
> complexity as much as possible. It may also be helpful to check the 
> implementation for errors or potential issues with the input parameters.
>
> On Tuesday, 28 February 2023 at 21:35:24 UTC+5:30 playe@gmail.com 
> wrote:
>
>> Hi all, 
>> I try to solve a non linear equation but it conitnue runiing since 48 
>> hours now.
>> ``
>> import sympy as sp
>>
>> X = sp.MatrixSymbol('X',2,2)
>> W = sp.MatrixSymbol('W',2,2)
>> Y = sp.MatrixSymbol('Y',1,2)
>> W_X = sp.MatMul(W,X.T)
>>
>> liste = []
>> for i in range(W_X.shape[0]):
>> row = []
>> for j in range(W_X.shape[1]):
>> row.append(1/(1+sp.exp(-W_X[i,j])))
>> 
>> liste.append(row)
>> sigmoid = sp.Matrix(liste)
>>
>> liste = []
>> for i in range(Y.shape[0]):
>> row = []
>> for j in range(Y.shape[1]):
>> row.append(sp.log(Y[i,j]/(1-Y[i,j])))
>> liste.append(row)
>> log_Y = sp.Matrix(liste)
>>
>> test = sigmoid.inv()
>>
>> W2 = sp.MatMul(log_Y,test).as_explicit()
>>
>> liste = []
>> for index in range(W2.shape[1]):
>> for i in range(W.shape[0]):
>> for j in range(W.shape[1]):
>> liste.append(W2[index].diff(W[i,j]))
>> 
>> 
>> p = sp.solve(liste,list(W),check=False,manual=True)
>> ```
>>
>>
>> Do someone have an idea where i am wrong or if it is from sympy ?
>>
>> Thanks a lot 
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/34d83d67-0cb4-4ec7-b314-b6be36492be9n%40googlegroups.com.


Re: [sympy] sort expression for power series

2023-03-06 Thread Chris Smith
When I pointed out " This gives an error. You have to split the ordered 
terms into coefficients and factors first with `as_coeff_Mul()`" it 
identified why the previous answer was incorrect and gave this modified 
line:

expList = [(round(coeff, 5), t.as_poly()) for coeff, t in 
(term.as_coeff_Mul() for term in expList)]  

On Monday, March 6, 2023 at 9:08:45 PM UTC-6 Chris Smith wrote:

> I thought I would try this in ChatGPT since one of the answers sounded 
> gpt-ish. Quite surprised by the results in terms of level of detail about 
> what is going on in the routine that it presented. I marked the line that 
> was needed to make the function of the gpt-answer work:
>
> def printSeries(expIn):
>  expOut = expIn
>  expOut = collect(expOut, t)
>  expList = expOut.as_ordered_terms() # convert expression to list
>  expList = [i.as_coeff_Mul() for i in expList]  # < needed this 
> line
>  expList = [(t, round(coeff, 5)) for coeff, t in expList] # round 
> coefficients to 5 digits
>  expList.sort(key=lambda term: term[0].as_poly().degree(), 
> reverse=True) # sort terms by degree of t
>  expStr = ' '.join([f"{coeff:.5g} {t}" for t, coeff in expList]) # 
> format terms as strings
>  print(expStr)
>
> /c
> On Monday, March 6, 2023 at 10:45:51 AM UTC-6 Oscar wrote:
>
>> The degree function can be imported like 
>>
>> from sympy import degree 
>>
>> It is used to get the degree of a polynomial expression. 
>>
>> On Mon, 6 Mar 2023 at 14:53, Thomas Ligon  wrote: 
>> > 
>> > Thanks! That's a big help, but I am not finished yet. Here is where I 
>> am now: 
>> > 
>> > Step 1: Use your code. 
>> > name 'StrPrinter' is not defined 
>> > changed StrPrinter to sympy.printing.str.StrPrinter 
>> > https://docs.sympy.org/latest/modules/printing.html 
>> > name 'degree' is not defined 
>> > I tried key=sympy.printing.str.degree, but that gave me 
>> > module 'sympy.printing.str' has no attribute 'degree' 
>> > Then I tried key=sympy.degree and that worked. 
>> > Now the two series are being printed in the desired order. 
>> > 
>> > Step 2: Use latex 
>> > I tried changing print(doprint(series)) to 
>> print(latex(doprint(series))) 
>> > and the result is 
>> > \mathtt{\text{-2.565*t + 147.94*t**3 - 2867.7*t**5}} 
>> > which doesn't format correctly in Word's equation editor or in 
>> www.overleaf.com, 
>> > where it formats correctly, but issues an error 
>> > Undefined control sequence. 
>> > LaTeX Error: \mathtt allowed only in math mode. 
>> > I tried changing the string to 
>> > -2.565*t + 147.94*t**3 - 2867.7*t**5 
>> > and this formats correctly in Word and in Overleaf. 
>> > Then I tried 
>> > doprint = 
>> sympy.printing.latex.LatexPrinter(settings={'order':'none'}).doprint 
>> > but that throws an exception 
>> > '_PrintFunction' object has no attribute 'LatexPrinter' 
>> > even though the class 
>> > class sympy.printing.latex.LatexPrinter(settings=None) 
>> > is defined in 
>> > 
>> https://docs.sympy.org/latest/modules/printing.html#module-sympy.printing.latex
>>  
>> > 
>> > Still to do: 
>> > Learn how this works. 
>> > Learn why evalf(5) works. The documentation at 
>> > https://docs.sympy.org/latest/modules/evalf.html 
>> > doesn't make it clear to me what the possible arguments are and what 
>> they mean. 
>> > Find documentation for sorted and key=degree. 
>> > I searched for sympy degree and found polynomials and angles, but still 
>> don't understand this one. 
>> > 
>> > 
>> > On Sunday, March 5, 2023 at 8:53:32 PM UTC+1 Oscar wrote: 
>> > 
>> > On Sun, 5 Mar 2023 at 08:32, Thomas Ligon  wrote: 
>> > > 
>> > > I have a lot of power series that look like this (but going up to 
>> t**12): 
>> > > exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 - 
>> 2.56500070531002*t 
>> > > While trying to gain some insight into the mathematics that creates 
>> them, I want to print a shorter version, such as 
>> > > - 2.565 t + 147.93872 t**3- 2867.70036 t**5 
>> > > but the best I have achieved is 
>> > > - 2867.70036 t^{5} + \left(147.93872 t^{3} - 2.565 t\right) 
>> > > Rounding the numbers was easy, but I would prefer to round to 5 
>> digits total, not 5 digits after the decimal point. I was able to convert 
>> the expression to a list and sort the list, but when I converted the list 
>> back to an expression, I didn't succeed in producing the order I wanted. 
>> > 
>> > You can get 5 digits by using exp1.evalf(5). 
>> > 
>> > It is surprisingly difficult to control the order of terms in sympy's 
>> > printing functionality but it is possible: 
>> > 
>> > In [1]: exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 - 
>> > 2.56500070531002*t 
>> > 
>> > In [2]: doprint = StrPrinter(settings={'order':'none'}).doprint 
>> > 
>> > In [3]: series = Add(*sorted(exp1.evalf(5).args, key=degree), 
>> evaluate=False) 
>> > 
>> > In [4]: print(doprint(series)) 
>> > -2.565*t + 147.94*t**3 - 2867.7*t**5 
>> > 
>> > There are two steps to controlling the order: 
>> > 
>> > - 

Re: [sympy] sort expression for power series

2023-03-06 Thread Chris Smith
I thought I would try this in ChatGPT since one of the answers sounded 
gpt-ish. Quite surprised by the results in terms of level of detail about 
what is going on in the routine that it presented. I marked the line that 
was needed to make the function of the gpt-answer work:

def printSeries(expIn):
 expOut = expIn
 expOut = collect(expOut, t)
 expList = expOut.as_ordered_terms() # convert expression to list
 expList = [i.as_coeff_Mul() for i in expList]  # < needed this line
 expList = [(t, round(coeff, 5)) for coeff, t in expList] # round 
coefficients to 5 digits
 expList.sort(key=lambda term: term[0].as_poly().degree(), 
reverse=True) # sort terms by degree of t
 expStr = ' '.join([f"{coeff:.5g} {t}" for t, coeff in expList]) # 
format terms as strings
 print(expStr)

/c
On Monday, March 6, 2023 at 10:45:51 AM UTC-6 Oscar wrote:

> The degree function can be imported like
>
> from sympy import degree
>
> It is used to get the degree of a polynomial expression.
>
> On Mon, 6 Mar 2023 at 14:53, Thomas Ligon  wrote:
> >
> > Thanks! That's a big help, but I am not finished yet. Here is where I am 
> now:
> >
> > Step 1: Use your code.
> > name 'StrPrinter' is not defined
> > changed StrPrinter to sympy.printing.str.StrPrinter
> > https://docs.sympy.org/latest/modules/printing.html
> > name 'degree' is not defined
> > I tried key=sympy.printing.str.degree, but that gave me
> > module 'sympy.printing.str' has no attribute 'degree'
> > Then I tried key=sympy.degree and that worked.
> > Now the two series are being printed in the desired order.
> >
> > Step 2: Use latex
> > I tried changing print(doprint(series)) to print(latex(doprint(series)))
> > and the result is
> > \mathtt{\text{-2.565*t + 147.94*t**3 - 2867.7*t**5}}
> > which doesn't format correctly in Word's equation editor or in 
> www.overleaf.com,
> > where it formats correctly, but issues an error
> > Undefined control sequence.
> > LaTeX Error: \mathtt allowed only in math mode.
> > I tried changing the string to
> > -2.565*t + 147.94*t**3 - 2867.7*t**5
> > and this formats correctly in Word and in Overleaf.
> > Then I tried
> > doprint = 
> sympy.printing.latex.LatexPrinter(settings={'order':'none'}).doprint
> > but that throws an exception
> > '_PrintFunction' object has no attribute 'LatexPrinter'
> > even though the class
> > class sympy.printing.latex.LatexPrinter(settings=None)
> > is defined in
> > 
> https://docs.sympy.org/latest/modules/printing.html#module-sympy.printing.latex
> >
> > Still to do:
> > Learn how this works.
> > Learn why evalf(5) works. The documentation at
> > https://docs.sympy.org/latest/modules/evalf.html
> > doesn't make it clear to me what the possible arguments are and what 
> they mean.
> > Find documentation for sorted and key=degree.
> > I searched for sympy degree and found polynomials and angles, but still 
> don't understand this one.
> >
> >
> > On Sunday, March 5, 2023 at 8:53:32 PM UTC+1 Oscar wrote:
> >
> > On Sun, 5 Mar 2023 at 08:32, Thomas Ligon  wrote:
> > >
> > > I have a lot of power series that look like this (but going up to 
> t**12):
> > > exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 - 
> 2.56500070531002*t
> > > While trying to gain some insight into the mathematics that creates 
> them, I want to print a shorter version, such as
> > > - 2.565 t + 147.93872 t**3- 2867.70036 t**5
> > > but the best I have achieved is
> > > - 2867.70036 t^{5} + \left(147.93872 t^{3} - 2.565 t\right)
> > > Rounding the numbers was easy, but I would prefer to round to 5 digits 
> total, not 5 digits after the decimal point. I was able to convert the 
> expression to a list and sort the list, but when I converted the list back 
> to an expression, I didn't succeed in producing the order I wanted.
> >
> > You can get 5 digits by using exp1.evalf(5).
> >
> > It is surprisingly difficult to control the order of terms in sympy's
> > printing functionality but it is possible:
> >
> > In [1]: exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 -
> > 2.56500070531002*t
> >
> > In [2]: doprint = StrPrinter(settings={'order':'none'}).doprint
> >
> > In [3]: series = Add(*sorted(exp1.evalf(5).args, key=degree), 
> evaluate=False)
> >
> > In [4]: print(doprint(series))
> > -2.565*t + 147.94*t**3 - 2867.7*t**5
> >
> > There are two steps to controlling the order:
> >
> > - Ordering the terms in the expression itself (the series variable 
> above).
> > - Getting the printer to respect that ordering ('order':'none').
> >
> > It should be possible to set order=None with init_printing and that
> > does work for the pretty printer but not for string printing (i.e.
> > print(expr) or str(expr)).
> >
> > --
> > Oscar
> >
> > --
> > You received this message because you are subscribed to the Google 
> Groups "sympy" group.
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to sympy+un...@googlegroups.com.
> > To view this discussion on the web 

Re: [sympy] sort expression for power series

2023-03-06 Thread Oscar Benjamin
The degree function can be imported like

from sympy import degree

It is used to get the degree of a polynomial expression.

On Mon, 6 Mar 2023 at 14:53, Thomas Ligon  wrote:
>
> Thanks! That's a big help, but I am not finished yet. Here is where I am now:
>
> Step 1: Use your code.
> name 'StrPrinter' is not defined
> changed StrPrinter to sympy.printing.str.StrPrinter
> https://docs.sympy.org/latest/modules/printing.html
> name 'degree' is not defined
> I tried key=sympy.printing.str.degree, but that gave me
> module 'sympy.printing.str' has no attribute 'degree'
> Then I tried key=sympy.degree and that worked.
> Now the two series are being printed in the desired order.
>
> Step 2: Use latex
> I tried changing print(doprint(series)) to print(latex(doprint(series)))
> and the result is
> \mathtt{\text{-2.565*t + 147.94*t**3 - 2867.7*t**5}}
> which doesn't format correctly in Word's equation editor or in 
> www.overleaf.com,
> where it formats correctly, but issues an error
> Undefined control sequence.
> LaTeX Error: \mathtt allowed only in math mode.
> I tried changing the string to
> -2.565*t + 147.94*t**3 - 2867.7*t**5
> and this formats correctly in Word and in Overleaf.
> Then I tried
> doprint = sympy.printing.latex.LatexPrinter(settings={'order':'none'}).doprint
> but that throws an exception
> '_PrintFunction' object has no attribute 'LatexPrinter'
> even though the class
> class sympy.printing.latex.LatexPrinter(settings=None)
> is defined in
> https://docs.sympy.org/latest/modules/printing.html#module-sympy.printing.latex
>
> Still to do:
> Learn how this works.
> Learn why evalf(5) works. The documentation at
> https://docs.sympy.org/latest/modules/evalf.html
> doesn't make it clear to me what the possible arguments are and what they 
> mean.
> Find documentation for sorted and key=degree.
> I searched for sympy degree and found polynomials and angles, but still don't 
> understand this one.
>
>
> On Sunday, March 5, 2023 at 8:53:32 PM UTC+1 Oscar wrote:
>
> On Sun, 5 Mar 2023 at 08:32, Thomas Ligon  wrote:
> >
> > I have a lot of power series that look like this (but going up to t**12):
> > exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 - 2.56500070531002*t
> > While trying to gain some insight into the mathematics that creates them, I 
> > want to print a shorter version, such as
> > - 2.565 t + 147.93872 t**3- 2867.70036 t**5
> > but the best I have achieved is
> > - 2867.70036 t^{5} + \left(147.93872 t^{3} - 2.565 t\right)
> > Rounding the numbers was easy, but I would prefer to round to 5 digits 
> > total, not 5 digits after the decimal point. I was able to convert the 
> > expression to a list and sort the list, but when I converted the list back 
> > to an expression, I didn't succeed in producing the order I wanted.
>
> You can get 5 digits by using exp1.evalf(5).
>
> It is surprisingly difficult to control the order of terms in sympy's
> printing functionality but it is possible:
>
> In [1]: exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 -
> 2.56500070531002*t
>
> In [2]: doprint = StrPrinter(settings={'order':'none'}).doprint
>
> In [3]: series = Add(*sorted(exp1.evalf(5).args, key=degree), evaluate=False)
>
> In [4]: print(doprint(series))
> -2.565*t + 147.94*t**3 - 2867.7*t**5
>
> There are two steps to controlling the order:
>
> - Ordering the terms in the expression itself (the series variable above).
> - Getting the printer to respect that ordering ('order':'none').
>
> It should be possible to set order=None with init_printing and that
> does work for the pretty printer but not for string printing (i.e.
> print(expr) or str(expr)).
>
> --
> Oscar
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sympy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/d48be44a-e979-41fd-aa37-1fabadde9b7an%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxQbSqDVa0a3hzhNuKTBPEZmYYoXdNvvky33%2BdkWk3V7GA%40mail.gmail.com.


Re: [sympy] sort expression for power series

2023-03-06 Thread Thomas Ligon
Thanks! That's a big help, but I am not finished yet. Here is where I am 
now:

Step 1: Use your code.
name 'StrPrinter' is not defined
changed StrPrinter to sympy.printing.str.StrPrinter
https://docs.sympy.org/latest/modules/printing.html
name 'degree' is not defined
I tried key=sympy.printing.str.degree, but that gave me
module 'sympy.printing.str' has no attribute 'degree'
Then I tried key=sympy.degree and that worked.
Now the two series are being printed in the desired order.

Step 2: Use latex
I tried changing print(doprint(series)) to print(latex(doprint(series)))
and the result is
\mathtt{\text{-2.565*t + 147.94*t**3 - 2867.7*t**5}}
which doesn't format correctly in Word's equation editor or in 
www.overleaf.com,
where it formats correctly, but issues an error
Undefined control sequence.
LaTeX Error: \mathtt allowed only in math mode.
I tried changing the string to 
-2.565*t + 147.94*t**3 - 2867.7*t**5
and this formats correctly in Word and in Overleaf.
Then I tried
doprint = 
sympy.printing.latex.LatexPrinter(settings={'order':'none'}).doprint
but that throws an exception
'_PrintFunction' object has no attribute 'LatexPrinter'
even though the class
class sympy.printing.latex.LatexPrinter(settings=None)
is defined in
https://docs.sympy.org/latest/modules/printing.html#module-sympy.printing.latex

Still to do:
Learn how this works.
Learn why evalf(5) works. The documentation at
https://docs.sympy.org/latest/modules/evalf.html
doesn't make it clear to me what the possible arguments are and what they 
mean.
Find documentation for sorted and key=degree. 
I searched for sympy degree and found polynomials and angles, but still 
don't understand this one.


On Sunday, March 5, 2023 at 8:53:32 PM UTC+1 Oscar wrote:

On Sun, 5 Mar 2023 at 08:32, Thomas Ligon  wrote: 
> 
> I have a lot of power series that look like this (but going up to t**12): 
> exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 - 
2.56500070531002*t 
> While trying to gain some insight into the mathematics that creates them, 
I want to print a shorter version, such as 
> - 2.565 t + 147.93872 t**3- 2867.70036 t**5 
> but the best I have achieved is 
> - 2867.70036 t^{5} + \left(147.93872 t^{3} - 2.565 t\right) 
> Rounding the numbers was easy, but I would prefer to round to 5 digits 
total, not 5 digits after the decimal point. I was able to convert the 
expression to a list and sort the list, but when I converted the list back 
to an expression, I didn't succeed in producing the order I wanted. 

You can get 5 digits by using exp1.evalf(5). 

It is surprisingly difficult to control the order of terms in sympy's 
printing functionality but it is possible: 

In [1]: exp1 = -2867.70035529489*t**5 + 147.938724526848*t**3 - 
2.56500070531002*t 

In [2]: doprint = StrPrinter(settings={'order':'none'}).doprint 

In [3]: series = Add(*sorted(exp1.evalf(5).args, key=degree), 
evaluate=False) 

In [4]: print(doprint(series)) 
-2.565*t + 147.94*t**3 - 2867.7*t**5 

There are two steps to controlling the order: 

- Ordering the terms in the expression itself (the series variable above). 
- Getting the printer to respect that ordering ('order':'none'). 

It should be possible to set order=None with init_printing and that 
does work for the pretty printer but not for string printing (i.e. 
print(expr) or str(expr)). 

-- 
Oscar 

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/d48be44a-e979-41fd-aa37-1fabadde9b7an%40googlegroups.com.


[sympy] Introduction and Interests

2023-03-06 Thread Himanshu Singh
Hello Everyone,
I am Himanshu Singh (github username: HimanshuSingh-all 
), I am currently pursuing my MSc. in 
Physics from Indian Institute of Technology, Guwahati. I am a beginner in 
open source. 

Going through the ideas list I would like to contribute to the *Classical 
Mechanics: Efficient Equation of Motion Generation with Python  *project. 
Any pointers on where I should start and what should I do apart from 
familiarizing myself with sympy which will be specific to the project. Any 
good first steps to contributing will also be appreciated.

Also I am not that familiar with the etiquette of open source and in case I 
would like to contribute to fixing an issue, Do I need to get it assigned 
to me first before I work on it?

As again any help/advice will me much appreciated. I hope to learn a lot 
from this.

Yours truly,
Himanshu Singh

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/3929b38c-4686-46a6-9009-03c98c0883fdn%40googlegroups.com.