0

Let's say that I have a quartic equation of the form:

a0x^4+a1x^3+a2x^2+a3x+a4=0

I know I can use numpy roots method to solve for a quartic equations, but I want the coefficients to change according to a rule, let's say that they depend on a parameter x, which takes values 1,2,...,10.

For example we have the coefficients

a0=1, a1=3q^2, a2=4q, a3=sqrt(q), a4=q

and want to obtain the solutions, in 8 columns, four real parts and four imaginary parts for each value of q, as q is changing, form 1 to 10.

For example, for x=1,

import numpy as np
q=1
a0=1
a1=3*q^2
a2=4*q 
a3=np.sqrt(q) 
a4=q
coeff=[a0, a1, a2, a3, a4]
np.roots(coeff)

The output would be:

array([-0.39002422+1.84237253j, -0.39002422-1.84237253j,
       -0.10997578+0.51949688j, -0.10997578-0.51949688j])

Is it possible to get the solutions, for all ranges of q, into a .csv format, in 9 columns, the first one for the value of q and than for each solution, a column for the real and imaginary part?

Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
  • For any given `q` there will be exactly `n` (polynomial degree, n=4 in your example) complex solutions for your equation. So could you clarify your question, what do you mean by "all ranges of x"? – Anvar Kurmukov Aug 15 '20 at 10:49
  • 1
    See https://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file – Stefan Aug 15 '20 at 11:00
  • @AnvarKurmukov That was a typo. I meant all rages of q – Maxtron Moon Aug 15 '20 at 14:08
  • @Stefan First I need to get the right output. As you can see, the output doesn't separate real from imaginary part with a comma, plus I need an extra columns to show the values of q... – Maxtron Moon Aug 15 '20 at 14:34
  • @MaxtronMoon as someone just remarked over at https://codereview.stackexchange.com/questions/247981/how-to-clean-the-indexes-and-ideally-not-create-an-additional-array, ^ is xor in Python, it is not for exponential computation, you want to use ** instead. – Uberhumus Aug 16 '20 at 19:37

1 Answers1

0

Basically the idea is to create a new array and export that. I could think of several ways to do this, here's one that I implemented. This method was chosen because it didn't require additional imports, and because it uses an answer that was linked in a comment to the question

import numpy as np
export = np.empty([10,9])
for q in range(1,11):
    a0=1
    a1=3*q^2
    a2=4*q 
    a3=np.sqrt(q) 
    a4=q
    coeff=[a0, a1, a2, a3, a4]
    answer = np.roots(coeff)
    export[q-1, 0] = q
    for _ in range(len(answer)):
        export[q-1, 2*_+1] = answer[_].real
        export[q-1, 2*(_+1)] = answer[_].imag
with open("/tmp/latest_foo.csv", "ab") as output_file:
    np.savetxt(output_file, export, delimiter=",")

Other ways you could do this would be creating a regular array rather than an np.ndarray and importing csv to write to the file, or using both "+" and "-" as delimiters and writing that to a new array or maybe directly to the csv. If you need a different format for the output I recommend looking at the formatting options available in numpy.ndarray.tofile.

Uberhumus
  • 921
  • 1
  • 13
  • 24
  • Thank you, but it doesn't give the right structure, it's all in one column instead of 9 columns., And could you add the appropriate loop form that would add the data for each q, I'm new to this and very confused... – Maxtron Moon Aug 15 '20 at 22:20
  • I'm not sure I understand, can you give an example of the desired output? – Uberhumus Aug 15 '20 at 22:30
  • In your answer,for q=1, the solutions appear in one column and 9 rows, I would like them to appear in one row and 9 columns, and than for q=2, to have another row, and q=3 another row and so on, till q=10... Thank you for your time... – Maxtron Moon Aug 16 '20 at 04:44
  • Thank you very much, this solves the problem, but I wonder something. I have another similar case, where the step is a float number, and with this code, I get an error. If the step is something like "for q in np.arange(0.01, 10, 0.5):", I get this error "only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices". Is there something I can do about it? Thank you so much for your work... – Maxtron Moon Aug 16 '20 at 13:31
  • @MaxtronMoon what is the use case and what have you tried? – Uberhumus Aug 16 '20 at 17:19