Is it possible to get polynomial multiplyiers?

54 views
Skip to first unread message

Michał Pawłowski

unread,
Feb 22, 2021, 6:44:17 AM2/22/21
to sympy
Hi.

I'd like to get polynomial qualifiers returned as array, in example:

getQualifiers("4*x**2+8*x+4")
[4, 8, 4]

or

getQualifiers("4*x**2+4")
[4, 0, 4]

Is it possible?

Thanks
Mike

Javier Arántegui

unread,
Feb 22, 2021, 6:56:57 AM2/22/21
to sy...@googlegroups.com
Hi!

Poly(4*x**2+8*x+4).all_coeffs()

Javier

El 22 feb 2021, a las 12:44, Michał Pawłowski <michal.bozyd...@gmail.com> escribió:

Hi.
--
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 visit https://groups.google.com/d/msgid/sympy/51ffa1bd-3654-40b5-a1c2-dc650b277222n%40googlegroups.com.

Michał Pawłowski

unread,
Feb 22, 2021, 10:39:09 AM2/22/21
to sympy
Thank you so much. But is it also possible to get formula from such array?

I.e.
getFormula("[2, 0, 2]")
2*x**2+2

Thanks 
Mike 

Javier Arántegui

unread,
Feb 22, 2021, 1:26:24 PM2/22/21
to sy...@googlegroups.com
Hi!

I don’t know if Sympy has a method to do it. The truth is I don’t know much.

Using my limited knowledge I wrote a function to do it. I’m sure that it can be improve a lot but it works:

- - - - -
def newpoly(coefs):
    p = ""
    for i in range(len(coefs)):
        p += str(coefs[i]) + '*x**' + str(len(coefs)-i-1) + '+'
    p = p[:-1]
    return sympify(p)

newpoly([4,8,3])
- - - - -

Javier

El 22 feb 2021, a las 16:39, Michał Pawłowski <michal.bozyd...@gmail.com> escribió:

Thank you so much. But is it also possible to get formula from such array?

Oscar Benjamin

unread,
Feb 22, 2021, 1:30:10 PM2/22/21
to sympy
On Mon, 22 Feb 2021 at 15:39, Michał Pawłowski
<michal.bozyd...@gmail.com> wrote:
>
> Thank you so much. But is it also possible to get formula from such array?
>
> I.e.
> getFormula("[2, 0, 2]")
> 2*x**2+2

Why is the "array" given as a string (in quotes)?

If you have a list of the coefficients then you can construct the
expression using a list comprehension:

In [1]: a = [2, 0, 1]

In [2]: x = Symbol('x')

In [3]: sum(x**n * ai for n, ai in enumerate(reversed(a)))
Out[3]:
2
2⋅x + 1

You can also use Poly:

In [4]: Poly(a, x).as_expr()
Out[4]:
2
2⋅x + 1

--
Oscar

Javier Arántegui

unread,
Feb 22, 2021, 6:24:08 PM2/22/21
to sy...@googlegroups.com
Hi!

I knew my code was bad, but not so bad. Hahaha! I just wish I could remove my message from the archive... 

Sorry for the noise.

Javier

El 22 feb 2021, a las 19:26, Javier Arántegui <javier.a...@gmail.com> escribió:



Michał Pawłowski

unread,
Feb 22, 2021, 6:41:17 PM2/22/21
to sympy
Thank you all of you. Thank you so much.

Michał Pawłowski 

Reply all
Reply to author
Forward
0 new messages