0

I am receiving the right answer when I compute the Vandermonde coefficients of this matrix. However, the output matrix is reversed. It should be [6,-39,55,27] instead of [27,55,-39,6].

My output for my Vandermonde Matrix is flipped and the final solution c, is flipped.

import numpy as np
from numpy import linalg as LA


x = np.array([[4],[2],[0],[-1]])
f = np.array([[7],[29],[27],[-73]])

def main():

    A_matrix = VandermondeMatrix(x)
    print(A_matrix)
    c = LA.solve(A_matrix,f) #coefficients of Vandermonde Polynomial
    print(c)

def VandermondeMatrix(x):
    n = len(x)
    A = np.zeros((n, n))
    exponent = np.array(range(0,n))
    for j in range(n):
        A[j, :] = x[j]**exponent
    return A





if __name__ == "__main__":
    main()
kusz
  • 257
  • 1
  • 9
mehek
  • 17
  • 2

4 Answers4

0

np.flip(c)?

link to documentation

ba5h
  • 97
  • 1
  • 11
0

You could do print(c[::-1]) which will reverse the order of c. From How can I flip the order of a 1d numpy array?

kusz
  • 257
  • 1
  • 9
0

Just make the exponent range the other way around from the beginning, then you don't have to flip afterwards reducing runtime:

def VandermondeMatrix(x):
    n = len(x)
    A = np.zeros((n, n))
    exponent = np.array(range(n-1,-1,-1))
    for j in range(n):
        A[j, :] = x[j]**exponent
    return A

Out:

#A_matrix:
[[64. 16.  4.  1.]
 [ 8.  4.  2.  1.]
 [ 0.  0.  0.  1.]
 [-1.  1. -1.  1.]]

#c:
[[  6.]
 [-39.]
 [ 55.]
 [ 27.]]
LeoE
  • 1,939
  • 1
  • 7
  • 23
  • `exponent = np.arange(n)[::-1]` is shorter :) Array reversal with negative strides is not an expensive operation, where ever it's done. – hpaulj Mar 04 '20 at 17:59
  • I know, but it is an unnecessary operation, so why do it at all if you don't have to? ;) – LeoE Mar 04 '20 at 18:17
-1
In [3]: def VandermondeMatrix(x): 
   ...:     n = len(x) 
   ...:     A = np.zeros((n, n)) 
   ...:     exponent = np.array(range(0,n)) 
   ...:     for j in range(n): 
   ...:         A[j, :] = x[j]**exponent 
   ...:     return A 
   ...:                                                                                        
In [4]: x = np.array([[4],[2],[0],[-1]])                                                       
In [5]: VandermondeMatrix(x)                                                                   
Out[5]: 
array([[ 1.,  4., 16., 64.],
       [ 1.,  2.,  4.,  8.],
       [ 1.,  0.,  0.,  0.],
       [ 1., -1.,  1., -1.]])
In [6]: f = np.array([[7],[29],[27],[-73]])                                                    
In [7]: np.linalg.solve(_5,f)                                                                  
Out[7]: 
array([[ 27.],
       [ 55.],
       [-39.],
       [  6.]])

The result is a (4,1) array; reverse rows with:

In [9]: _7[::-1]                                                                               
Out[9]: 
array([[  6.],
       [-39.],
       [ 55.],
       [ 27.]])

Negative strides, [::-1] indexing is also used to reverse Python lists and strings.

In [10]: ['a','b','c'][::-1]                                                                   
Out[10]: ['c', 'b', 'a']
hpaulj
  • 201,845
  • 13
  • 203
  • 313
  • So you essentially answered with the same answer, as mkusz but with a lot of unnecessary intermediate results – LeoE Mar 04 '20 at 17:44
  • @LeoE, I tested the OP code in part because I wondered why he got the reversed answer. I saw that he was getting a 2d answer (rather than the 1d implied by question), but you dug in to further. In any case my answers tend to be wordy, aiming for understanding, and not just an immediate solution. – hpaulj Mar 04 '20 at 18:18