Python Forum
Inaccurate result when a vector is divided by a matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inaccurate result when a vector is divided by a matrix
#1
I'm relatively new in Python, I did a lots of programs in Octave but now try to transfer them in PyThon.
One of my program divide a vector by a matrix, strangely the results is very different from the one I
obtain from Octave. The Octave results looks OK because the output final result is in accordance with the real
result of the publish examples.

The Python program (reduce to the essential)
import numpy as np
a=np.array([10,10,10])
b=np.array([[1,1,1],[2,2,2],[3,3,3]])
print(a)
print(b)
c=np.divide(a,b)
print("np.divide(a,b) = ")
print (c)
The Python results:
Output:
[10 10 10] [[1 1 1] [2 2 2] [3 3 3]]
np.divide(a,b) =
Output:
[[10. 10. 10. ] [ 5. 5. 5. ] [ 3.33333333 3.33333333 3.33333333]]
The Octave results for the same operation:
Output:
0.71429 , 1.42847 , 2.14286
I Try different configuration of the vector (horizontal,Vertical)
Thanks for your help
Dennis
Reply
#2
Why do you use np.divide instead of c = a / b? Numpy array/ndarray class override standard (magic) Python methods and you can use
usual arithmetic operators, e.g. +, *, /, @ -- for matrix multiplication (row-by-column fashion).
I worked with MatLab for a long time ago, but it seems to me that a/b gives a solution of a system of linear equations in octave/matlab, e.g. it returns x, where b*x = a or something similar( I am not sure exactly). Maybe it returns a minimal norm solution in a least-squares sense. So, in Octave you got a solution of a SLAE (system of linear algebraic equations)
defined by corresponding matrices a and b. Consider using ./ in octave for element-wise division (not sure, however).
Reply
#3
I should have mention it, I did try c=a/b on Python but I get the same result. As I said the result on Octave is the right one.
Reply
#4
Ok,
if you try to execute the following code
import numpy as np
a = np.array([10, 10, 10])
b = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
a @ np.linalg.pinv(b)
you get
Output:
array([0.71428571, 1.42857143, 2.14285714])
np.linalg.pinv(x) returns Moore-Penrose inversion of a matrix x. x @ y denotes matrix product between x and y. Moore-Penrose inversion is used to obtain least squares solution for over-determined (or under-determined) linear systems. Octave knows: if you are trying to divide one matrix onto another, you are likely trying to solve system of linear equations. E.g.: Ax=b, then x = b / A; in Python you should do this: x = np.linalg.pinv(A) @ b.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 838 Jul-10-2023, 02:41 AM
Last Post: deanhystad
Question How to understand the vector/direction mason321 0 1,119 Dec-14-2021, 10:57 PM
Last Post: mason321
  How to find vector of a 3D plot mason321 0 1,019 Nov-13-2021, 05:05 PM
Last Post: mason321
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,384 May-03-2021, 06:30 AM
Last Post: Gribouillis
  How do I make a symmetric matrix from a column vector? leocsmith 3 3,691 Mar-30-2021, 10:17 AM
Last Post: leocsmith
  3D vector class with inheritance from 2D vector class buss0140 4 3,161 Dec-20-2020, 08:44 PM
Last Post: deanhystad
  Sume Every 10 element in the list and Divided of Sum quest_ 7 3,095 Nov-27-2020, 10:58 AM
Last Post: perfringo
  Issue with def norm in class Vector DimosG 4 2,499 Mar-26-2020, 05:03 PM
Last Post: DimosG
  Return all Values which can divided by 9 lastyle 2 1,845 Mar-16-2020, 09:22 PM
Last Post: lastyle
  Problem in creating a vector termo 11 3,927 Oct-10-2019, 03:09 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020