Home > Mobile >  Why am I getting wrong matrix
Why am I getting wrong matrix

Time:12-01

I'm trying to print the following matrix and vector,

A = np.array ([[2,1,4,1], [3,4,-1,-1] , [1,-4,1,5] , [2,-2,1,3]], float)
v = np.array([-4, 3, 9, 7], float)

But I'm getting this instead

A = 
[[ 1.   0.5  2.   0.5]
 [ 0.   1.  -2.8 -1. ]
 [-0.  -0.   1.  -0. ]
 [-0.  -0.  -0.   1. ]]

v = [-2.   3.6 -2.   1. ]

If I change from float to int I get the right matrix but then I gett error when trying to run the following code

for m in range(N):
    div = A[m,m]
    A[m,:] /= div
    v[m] /= div
UFuncTypeError: Cannot cast ufunc 'true_divide' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'

How can I solve tis problem?

CodePudding user response:

I guess right syntax is this?

import numpy as np
A = np.array ([[2,1,4,1], [3,4,-1,-1] , [1,-4,1,5] , [2,-2,1,3]], dtype = float)
print(A)

Gives #

[[ 2.  1.  4.  1.]
 [ 3.  4. -1. -1.]
 [ 1. -4.  1.  5.]
 [ 2. -2.  1.  3.]]
  • Related