I want to get the coefficients of a polynomial and store them in an array, for example:
23x³+5x²+10x+5
arr[0]=23
arr[1]=5
arr[2]=10
arr[3]=5
I know how to evaluate the functions, but in this case I just want to get the coefficients, in python
Let's suppose that the input of the problem is a character string that represents a polynomial of arbitrary degree (maximum 9), like the following:
The problem of detecting the coefficients can be attacked by using a regular expression that looks for digits optionally followed by one
x
that is optionally followed by a unicode "exponent" character. The following regular expression does this, although it becomes more complicated than necessary for wanting to also deal with the cases in which the coefficient or the exponent is not written, because it is 1:and incidentally it captures in different capture groups the digits that appear before the
x
(and an optional one-
in case the coefficient is negative), its ownx
(which is actually optional so that it also works with the independent term) and the Unicode digits of the exponent. The digits in front of the x are optional as well as the digits of the exponent, as they may not appear as in"x+1"
.After applying that regular expression, we iterate through the resulting groups to save what we find in a dictionary. The key of the dictionary will be the exponent and the value will be the corresponding coefficient. Some cases must be detected and treated in a special way:
These special cases mess up the code a bit, which would look like this:
The line
exp = ord(exp) & 0x000F
is a "dirty trick". It happens that the Unicode characters of the exponents, like the ASCII codes of the digits, are ordered in such a way that their last hexadecimal figure coincides with the number they represent. For example, the Unicode for ² is 0x00B2, for ³ is 0x00B3, for ⁴ is 0x2074, etc, as you can see hereTaking advantage of this fact, through
&
logic I set all the bits of the code to 0 except the last 4, and that therefore gives me the figure they represent.The result is this dictionary:
All that remains is to convert that dictionary into a list so that the index is the exponent and the value is the coefficient. For the thing to be general enough, we first look at what is the largest of the keys (in this case 3), since that gives us the degree of the polynomial and therefore the necessary size of the resulting list. The rest is just filling in:
The result in this case is:
In the question the user asks for the opposite order, I suppose by mistake, since the logical thing is that the index
[i]
of this list gives us the coefficient of thei
-th power ofx
. In any case, if you really need it the other way around, just do a finalcoeficientes.reverse()
Let's put all of the above into one function:
Note The algorithm works also if the terms are out of order, if some are missing (the corresponding coefficient will be zero) or if there are gaps between coefficients. It doesn't work instead if there is a negative coefficient with a space between the minus sign and the number.
Examples of inputs and their corresponding outputs:
I've tried more, and it's worked, but I may have missed some other "special case". Of course it doesn't do symbolic operations, so it won't work with a polynomial like
"x+x"
(doesn't add the x's) either.Update
If instead of unicode exponents the exponents come in the form
x**N
orx^N
, the regular expression gets a bit more complicated (to admit both variants) and would be:That is, the function would be the following:
Trying some examples seems to work fine: