Sage equivalent for element-wise Matlab operators

713 views
Skip to first unread message

Ryan Hinton

unread,
Mar 21, 2008, 11:34:18 AM3/21/08
to sage-s...@googlegroups.com
I am trying to do the element-by-element division of two Matrix
objects. In Matlab I do this with the ./ operator. Is there an
equivalent in Sage?

The solutions I have found so far are (1) a double-for loop and (2)
converting to numpy arrays, divide (which is element-wise by default in
NumPy), then convert back to Matrix. Neither of these solutions are
particularly appealing to me. Is there a better way?

Thanks!

---
Ryan Hinton
iobass, domain email.com

Michael

unread,
Mar 21, 2008, 1:23:39 PM3/21/08
to sage-support
Octave has the same ./ operator. If you have Octave in your Sage
installation (this works at sagenb.org):

z = octave("[1 2 3] ./ [4 5 6]")
z
==> 0.25 0.4 0.5


If you want to write it from scratch, I don't think you need a double
loop (this works at sagenb.org):

def divel(m,n):
ans = []
for i,j in zip(m,n):
ans.append(i/j)
return ans
m = [1,2,3]
n = [4,5,6]
divel(m,n)
==> [1/4, 2/5, 1/2]

William Stein

unread,
Mar 21, 2008, 1:56:33 PM3/21/08
to sage-s...@googlegroups.com

Element divide is very unnatural from an algebraic point of view, which
is why we never even thought to implement it. Here's an example
of a 1 liner that does implement it, along with example usage.

sage: def matrix_eltdiv(A, B):
... return A.parent()([a/b for a, b in zip(A.list(), B.list())])
sage: A = matrix(QQ,3,5,[1..15]); B = matrix(QQ,3,5,[5..19])
sage: print A
sage: print B
[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
sage: matrix_eltdiv(A,B)
[ 1/5 1/3 3/7 1/2 5/9]
[ 3/5 7/11 2/3 9/13 5/7]
[11/15 3/4 13/17 7/9 15/19]


If some other sage developers think it would be a good thing
for sage matrices to have this built in (I think it would be),
they should create a trac ticket for it. It would certainly be
faster if it were built in.

William

Michael

unread,
Mar 21, 2008, 3:46:24 PM3/21/08
to sage-support
@Ryan: Will's one liner is much better than my five liner which
(cough, cough) doesn't actually work for Sage matrices.

@Will: Whoever wants to implement ./ in Sage may also want to think
about the other element operations in Octave, some of which are
illustrated here:

octave.exe:56> [1 2 3] .* [1 2 3]
ans =

1 4 9

octave.exe:57> [1 2 3] .+ [1 2 3]
ans =

2 4 6

octave.exe:58> [1 2 3] .- [1 2 3]
ans =

0 0 0

octave.exe:59> [1 2 3] .\ [ 1 2 3 ]
ans =

1 1 1

octave.exe:60> [1 2 3] .\ [2 4 6]
ans =

2 2 2

octave.exe:61> [1 2 3] .^ [1 2 3]
ans =

1 4 27

Ryan Hinton

unread,
Mar 22, 2008, 11:41:01 AM3/22/08
to sage-support
Given that there is no difference between "+" and ".+", "-" and ".-",
I don't see much utility in implementing them. (I did not know they
existed.) And I have never used the element-wise left-divide,
either. But the multiplication, division, and exponentiation would be
very valuable in my work.

Is this something that could/should be added to the pre-processor and
mapped to a certain method? For example, "a ./ b" could be mapped to
"a.__elem_div__(b)", analogous to the way Python maps "/" to the
"__div__" method.

Jason Grout

unread,
Mar 22, 2008, 1:48:43 PM3/22/08
to sage-s...@googlegroups.com
Ryan Hinton wrote:
> Given that there is no difference between "+" and ".+", "-" and ".-",
> I don't see much utility in implementing them. (I did not know they
> existed.) And I have never used the element-wise left-divide,
> either. But the multiplication, division, and exponentiation would be
> very valuable in my work.
>
> Is this something that could/should be added to the pre-processor and
> mapped to a certain method? For example, "a ./ b" could be mapped to
> "a.__elem_div__(b)", analogous to the way Python maps "/" to the
> "__div__" method.


It looks like there was extensive discussion in python about
element-wise operators a few years ago. See PEP 225:

http://www.python.org/dev/peps/pep-0225/

For some discussion, see the threads pointed out in the PEP:

http://mail.python.org/pipermail/python-list/2000-July/thread.html#44442

http://mail.python.org/pipermail/python-list/2000-August/thread.html#46674

for example.

Jason

Reply all
Reply to author
Forward
0 new messages