Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 04:53
    PNX007 starred sympy/sympy
  • 03:43
    codecov[bot] commented #17870
  • 03:01
    oscarbenjamin closed #16680
  • 03:01
    oscarbenjamin commented #16680
  • 03:00

    oscarbenjamin on 1.5

    Update repr-printing of element… Fix doctests for elementwise fu… Merge pull request #17929 from … (compare)

  • 03:00
    oscarbenjamin closed #17929
  • 02:44
    codecov[bot] commented #17929
  • 02:42
    oscarbenjamin commented #17926
  • 02:41
    asmeurer commented #17926
  • 02:41
    oscarbenjamin commented #17926
  • 02:38
    eric-wieser commented #17926
  • 02:30
    oscarbenjamin commented #17921
  • 02:21
    oscarbenjamin commented #17922
  • 02:17
    oscarbenjamin commented #17926
  • 02:12
    oscarbenjamin commented #17927
  • 02:11
    oscarbenjamin labeled #17929
  • 02:11
    oscarbenjamin labeled #17929
  • 02:09
    oscarbenjamin labeled #17930
  • 02:09
    oscarbenjamin labeled #17917
  • 02:08
    oscarbenjamin labeled #17918
Kalevi Suominen
@jksuom
If no symbols are given, internally generated symbols will be used.
It seems that you can give the names that you would like to use instead of the generated names like tau0.
Igor Morgado
@igormorgado
@jksuom But I cannot specify the names,since I don't know how many free vars will exist.
@jksuom in fact, I don't even know the size of the matrix beforehand. It will depend on size of the grid loaded. can have 500 variables.
V Gladkova
@valglad

@igormorgado That seems like a good enough solution, though I would do the substitution like:

zeros = [(s, 0) for s in r_tmp.free_symbols]
solution_as_list = r_tmp.subs(zeros)

Not sure what you are trying to do with the numpy array part though

Igor Morgado
@igormorgado
@valglad nice approach.
@valglad The solution_as_list, didn't returns a list per se, but a set with a tuple, as in: {(-0.916666666666667, 0.708333333333333, 0.375, -0.208333333333333, 0.0416666666666667, 0)} of type sympy.sets.sets.FiniteSet.
Igor Morgado
@igormorgado
@valglad Regarding to the numpy line. I'm very sticky to numpy, since I oftern work with it. Maybe I will keep everything inside sympy, until get the final solution.
V Gladkova
@valglad

I see, so you need to turn it into a list, flatten it and make everything a float. You could use a comprehension again like

solution = np.array([e for e in r_tmp]).astype(float)

It may or may not be slightly faster but it looks shorter

Ah, wait, that doesn't flatten it. So you'll have to do what you did or use SymPy's flatten anyway... Never mind then

Actually,

solution = np.array(*[e for e in r_tmp]).astype(float)

should flatten it as well

Igor Morgado
@igormorgado
@valglad thank you.
Igor Morgado
@igormorgado

wow. :-( Given the matrix PI

array([[ -0.917,   0.042,   0.   ,   0.   ,  -1.   ],
       [  0.708,  -1.125,   0.042,   0.   ,   5.   ],
       [  0.375,   1.125,  -1.125,   0.042, -10.   ],
       [ -0.208,  -0.042,   1.125,  -1.125,  10.   ],
       [  0.042,   0.   ,  -0.042,   1.125,  -5.   ],
       [  0.   ,   0.   ,   0.   ,  -0.042,   1.   ]])

and the vector c

array([-1.   ,  0.   ,  0.   , -0.042,  1.083, -0.042])

the solution given by

linsolve(Matrix(np.c_[PI,c]))

returns an EmptySet()

while the same operation in Maple 16 returns non empty, and exactly

np.array([649/576, 143/192, 75/64, 551/576, -25/13824])

that in float is around

array([ 1.127,  0.745,  1.172,  0.957, -0.002])

Any ideas?

maple_answer.png
the example in maple is shown above
Igor Morgado
@igormorgado
just to make clear, the float points in my PI matrix are not exactly 3 digits precision, I'm just showing the first 3 most significant digits using: np.set_printoptions(precision=3)
for sake of this test it was created using the ratios, as in
PI = np.array([
    [-11/12, 1/24, 0, 0, -1],
    [ 17/24, -9/8, 1/24, 0 ,5],
    [3/8, 9/8, -9/8, 1/24, -10],
    [-5/24, -1/24, 9/8, -9/8, 10],
    [1/24, 0, -1/24, 9/8, -5],
    [0, 0, 0, -1/24, 1]
])
Ideas, are very likely welcome.
Igor Morgado
@igormorgado
Using np.linalg.lstsq(PI, c, rcond=-1)[0] returned the expected answer as: array([ 1.127, 0.745, 1.172, 0.957, -0.002])
Igor Morgado
@igormorgado
but still, I would like to know what I'm doing wrong on Sympy side.
Kalevi Suominen
@jksuom

It seems that your system is over-determined. It has 6 equations in 5 variables. It can have solutions only if the determinant of the extended system is 0.

>>> P = Matrix([[ -0.917,   0.042,   0.   ,   0.   ,  -1.   ],
...        [  0.708,  -1.125,   0.042,   0.   ,   5.   ],
...        [  0.375,   1.125,  -1.125,   0.042, -10.   ],
...        [ -0.208,  -0.042,   1.125,  -1.125,  10.   ],
...        [  0.042,   0.   ,  -0.042,   1.125,  -5.   ],
...        [  0.   ,   0.   ,   0.   ,  -0.042,   1.   ]])
>>> c = Matrix([-1.   ,  0.   ,  0.   , -0.042,  1.083, -0.042])
>>> Pc = P.row_join(c)
>>> Pc.det()
0.000998125750125132

In this example, the determinant not 0 because of the inaccuracy of floating point numbers. Hence there is no solution.

Kalevi Suominen
@jksuom
You can probably find a solution if you remove one of the equations.
Het Patel
@hetp111
is NumPy related to sympy?
i am learning python megacourse on udemy and it is also teaching numpy, so i was wondering if it might be useful
Igor Morgado
@igormorgado
@hetp111 not related but can work together.
V Gladkova
@valglad
The lack of solutions is definitely a precision issue - if you input all entries as Rational (e.g. Rational(1,2) instead of 1/2), then linsolve returns the same as Maple (though obviously that's awkward)
V Gladkova
@valglad

A potential solution could be to define

rationalize = lambda x: [nsimplify(e) for e in x]

(this will turn numbers from a list into rationals). And then do

PI = Matrix(map(rationalize, [
    [-11/12, 1/24, 0, 0, -1],
    [ 17/24, -9/8, 1/24, 0 ,5],
    [3/8, 9/8, -9/8, 1/24, -10],
    [-5/24, -1/24, 9/8, -9/8, 10],
    [1/24, 0, -1/24, 9/8, -5],
    [0, 0, 0, -1/24, 1]
]))

and similarly for c. I've just checked and this returns the right solutions

Igor Morgado
@igormorgado
@jksuom I cant remove any variable unfortunately.
@valglad Amazing! I will test.
Kalevi Suominen
@jksuom
Do not remove any variables. There are too few already. Remove one of the equations.
There are 6 rows and only 5 columns. One row could be omitted.
Igor Morgado
@igormorgado
@jksuom I can't either, they are strict contraints in my solution space.
Kalevi Suominen
@jksuom
If there is a solution, then the same solution is obtained after one equation is omitted.
Kalevi Suominen
@jksuom
In [7]: linsolve(Pc[:5, :])
Out[7]: 
{(1.12549831271091, 0.749071392102014, 1.16564957508439, 0.96140585029474, -0.
000620954287620833)}
Igor Morgado
@igormorgado
@valglad Thanks. rationalize function worked (in fact I was using Fractions.from_float() before, this work better). But I could not apply your lambda function to a 2-d matrix, just 1-d.
I have created mrationalize = lambda x: [nsimplify(e) for e in [r for r in x]] that I do not like much the fell.
V Gladkova
@valglad
Doing map(rationalize, list_of_lists) inside the Matrix call (like I did above when creating PI) would probably be easiest
Adoa Coturnix
@adoa

I have a problem with calculating eigenvectors for a simple 3x3 matrix in sympy. Trying to execute

q = symbols("q", positive=True)
m = Matrix([[-2, exp(-q), 1], [exp(q), -2, 1], [1, 1, -2]])
#m.eigenvects(simplify=True)
m.eigenvals()

results in very complicated expressions for the eigenvalues, trying to get the eigenvectors fails with a NotImplementedError. I expect the latter to be due to the former. However, when trying to do the same thing in Mathematica, I get much simpler expressions for everything. Is there some option / flag that I can set to have sympy compute these eigenvalues and eigenvectors? Is there currently some form of limitation within sympy that prevents me from doing this?

Het Patel
@hetp111
what are all the prerequisite i need to have before contributing to opensource (sympy) ? I have a pretty good hold of python now. Github is very confusing for me.
Aaron Meurer
@asmeurer
It helps to understand git/github, but generally the best way to learn them is to use them
@adoa does Mathematica give a simpler expression for the eigenvalues?
Oh apparently they can be simplified if you call simplify
Het Patel
@hetp111
@asmeurer Ty, do i need to learn git or github? is learning github enough? i have never done opensource before, should i start with sympy?
Aaron Meurer
@asmeurer
You need to learn the basics of git, to download the code and push it up to GitHub
@adoa it seems there is a simplify flag, but it doesn't work
Het Patel
@hetp111
@asmeurer can't i do it manually through github website?
Kalevi Suominen
@jksuom
I suspect that there is not enough support for simplifying hyperbolic functions.
This may help you
Aaron Meurer
@asmeurer
@adoa I opened sympy/sympy#15118 for your issue