Vector division by a matrix different results when executed in Octave or Python

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Vector division by a matrix different results when executed in Octave or Python

Denis Lessard
Hy I am trying to convert some of my Octaves programs to Python, but presently I am stoped by a very strange problem.
The results for a vector divided by a matrix on both system do not give me the same results.
Here is a simplified example of the problem:
On Python:

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 results:
a=[10 10 10]

b=[[1 1 1]
    [2 2 2]
    [3 3 3]]

np.divide(a,b) = 
 [[10.         10.         10.        ]
 [ 5.          5.          5.        ]
 [ 3.33333333  3.33333333  3.33333333]]

The results are the same if I used c=a/b

For octave I wrote the same small program (no numpy )
and when I use c=a/b the results are:
0.71429 , 1.42847 , 2.14286

The Octave results are the good one when I compare my program results with the web examples used 
to validate my program.

I did try different approach in Python like transpose etc but notting seems to approach the response given by Octave.

Thanks
Denis




Reply | Threaded
Open this post in threaded view
|

Re: Vector division by a matrix different results when executed in Octave or Python

mmuetzel
Am 25. Oktober 2019 um 14:44 Uhr schrieb "Denis Lessard":
> Hy I am trying to convert some of my Octaves programs to Python, but presently I am stoped by a very strange problem.
> The results for a vector divided by a matrix on both system do not give me the same results.
>  
> The Octave results are the good one when I compare my program results with the web examples used 
> to validate my program.

> I did try different approach in Python like transpose etc but notting seems to approach the response given by Octave.

This looks like a question you should ask on a Python mailing list. It looks like you know what to do to get the desired results in Octave.
From an Octave point of view, you might want to inspect the difference between the "/" and the "./" operators.

Markus


Reply | Threaded
Open this post in threaded view
|

Re: Vector division by a matrix different results when executed in Octave or Python

Mike Miller-4
In reply to this post by Denis Lessard
On Fri, Oct 25, 2019 at 12:44:53 +0000, Denis Lessard wrote:

> 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 results:
>
> a=[10 10 10]
>
>
> b=[[1 1 1]
>     [2 2 2]
>     [3 3 3]]
>
>
> np.divide(a,b) =
>  [[10.         10.         10.        ]
>  [ 5.          5.          5.        ]
>  [ 3.33333333  3.33333333  3.33333333]]
This is the same result as Octave's ./ operator, elementwise division.

> For octave I wrote the same small program (no numpy )
> and when I use c=a/b the results are:
> 0.71429 , 1.42847 , 2.14286

For the equivalent linear algebra in Python, use

    (np.linalg.pinv(b).T * np.matrix(a).T).T

--
mike



signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Vector division by a matrix different results when executed in Octave or Python

Thomas D. Dean-2
In reply to this post by Denis Lessard
On 10/25/19 5:44 AM, Denis Lessard wrote:

> Hy I am trying to convert some of my Octaves programs to Python, but
> presently I am stoped by a very strange problem.
> The results for a vector divided by a matrix on both system do not give
> me the same results.
> Here is a simplified example of the problem:
> On Python:
>
> 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 results:
>
> a=[10 10 10]
>
> b=[[1 1 1] [2 2 2] [3 3 3]]
>
> np.divide(a,b) = [[10. 10. 10. ] [ 5. 5. 5. ] [ 3.33333333 3.33333333
> 3.33333333]]
>
>
> The results are the same if I used c=a/b
>
> For octave I wrote the same small program (no numpy )
> and when I use c=a/b the results are:
> 0.71429 , 1.42847 , 2.14286
>
> The Octave results are the good one when I compare my program results
> with the web examples used
> to validate my program.
>
> I did try different approach in Python like transpose etc but notting
> seems to approach the response given by Octave.
>

If you are looking for python help, you need to ask in a python forum.

To produce the same result in octave, look at 'help ./'

Tom Dean



Reply | Threaded
Open this post in threaded view
|

Re: Vector division by a matrix different results when executed in Octave or Python

Denis Lessard
In reply to this post by Mike Miller-4
Hi Mike

I would like to thank you so much…. you help me a lot…..

Thanks again
Denis


> On Oct 25, 2019, at 13:54, Mike Miller <[hidden email]> wrote:
>
> On Fri, Oct 25, 2019 at 12:44:53 +0000, Denis Lessard wrote:
>> 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 results:
>>
>> a=[10 10 10]
>>
>>
>> b=[[1 1 1]
>>    [2 2 2]
>>    [3 3 3]]
>>
>>
>> np.divide(a,b) =
>> [[10.         10.         10.        ]
>> [ 5.          5.          5.        ]
>> [ 3.33333333  3.33333333  3.33333333]]
>
> This is the same result as Octave's ./ operator, elementwise division.
>
>> For octave I wrote the same small program (no numpy )
>> and when I use c=a/b the results are:
>> 0.71429 , 1.42847 , 2.14286
>
> For the equivalent linear algebra in Python, use
>
>    (np.linalg.pinv(b).T * np.matrix(a).T).T
>
> --
> mike