Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Successive Differences

2 views
Skip to first unread message

smandula

unread,
Dec 10, 2007, 9:06:36 AM12/10/07
to
Using Dyalog APL, how would you subtract each element in a Matrix from
the next element?
In a series by row only
Before
Matrix
1 12 3
24 35 36

After
Matrix
11 9 0
10 1 0

With Thanks


Martin Turner

unread,
Dec 10, 2007, 9:40:45 AM12/10/07
to
Are there a couple of mistypes in your "after matrix"?

Perhaps you mean
11 -9 0
11 1 0

Martin


"smandula" <sman...@idirect.com> wrote in message
news:c4d2ee9f-b160-4181...@d27g2000prf.googlegroups.com...

smandula

unread,
Dec 10, 2007, 10:17:32 AM12/10/07
to
Your right, I my haste to send.
I can shape a matrix by row either ascending or descending elements,
but can not subtract one element from another in series within that
row. It's not important
to have minus elements in the solution, but would be nice.
I think
(1 {downgrade}Matrix) - {neg}1{downgrade}Matrix ...
is close to a solution?

Veli-Matti

unread,
Dec 10, 2007, 11:46:15 AM12/10/07
to

(<negative>2 -/Matrix),0
- Veli-Matti

Martin Turner

unread,
Dec 10, 2007, 11:52:51 AM12/10/07
to
Standard APL solutions are

( (0 1 {drop}Matrix) - (0 {neg}1{drop}Matrix) ),0

or

({rho}Matrix){take} (0 1 {drop}Matrix) - (0 {neg}1{drop}Matrix

"smandula" <sman...@idirect.com> wrote in message

news:c8c67db2-a0f3-4bb6...@d21g2000prf.googlegroups.com...

Lance

unread,
Dec 10, 2007, 12:38:06 PM12/10/07
to

Should your after matrix look like:?


11 -9 0
11 1 0

If so, the way it's done in WinAPL is (¯2-/T),0

Lance

smandula

unread,
Dec 10, 2007, 2:38:06 PM12/10/07
to
Thanks everyone for your time.
The solutions are very much appreciatd

mcbo...@sonic.net

unread,
Dec 11, 2007, 1:35:52 PM12/11/07
to

why does negative 2 work? it's bad syntax to me. (high minus
2)matrix???? negation 2 also works.

Veli-Matti

unread,
Dec 11, 2007, 2:03:03 PM12/11/07
to

> why does negative 2 work? it's bad syntax to me. (high minus
> 2)matrix???? negation 2 also works.

In this case the reduce operator has two operands, and it's called
n-wise reduction, ie. the operand function (right operand) is used
for each sub-array whose length is <absolute>left operand. If the
left operand is negative, the sub-array is reversed before the right
operand function is called.

The axis of the operation may be given in braces after the operand,
ie. (-2)/[2]MAT means the second axis (rowwise).

The default axis for reduction is the last axis, so we don't need
the axis definition in the example.

The example matrix has two sub-matrices which both have two
columns (because of the -2 operand):
1 12 and 12 3
24 35 35 36

those matrices are reversed, because the left operand was negative
(quite handy with subtraction):
12 1 and 3 12
35 24 36 35

then the right operand function takes place
(12-1) and (3-12)
(35-24) (36-35)

and at last we get the result by joining the columns:
11 -9
11 1

NB. the result has one column less than the original, so we have to
add extra zero to get the same shape as the original matrix has

Hopefully this lightens the idea.

-wm

Stephen Taylor <editor@vector.org.uk>

unread,
Dec 12, 2007, 5:20:26 AM12/12/07
to

If you find the negative left operand counter-intuitive, you could
just commute the subtract function. These are equivalent:

¯2 -/↑(1 12 3 )(24 35 36)
2(-⍨)/↑(1 12 3 )(24 35 36)

(for non-Unicoders)

¯2 - /{take}(1 12 3 )(24 35 36)
2(-{commute})/{take}(1 12 3 )(24 35 36)

"Negation also works". Yes, by reversing the sign of all the results.
Same answer, but quite different mechanism from using ¯2.

SJT

Phil Last

unread,
Dec 12, 2007, 6:06:40 AM12/12/07
to
On Dec 12, 10:20 am, "Stephen Taylor <edi...@vector.org.uk>"

(-n)f/⍵ ←→ n f⍨/⍵ ⍝ Stephen's equivalence
But beware, beyond the special case of n=2 the equivalence only holds
for associative functions.
¯2-/1 9 2 8 3 7 4 6 5
8 ¯7 6 ¯5 4 ¯3 2 ¯1
2-⍨/1 9 2 8 3 7 4 6 5
8 ¯7 6 ¯5 4 ¯3 2 ¯1
¯3-/1 9 2 8 3 7 4 6 5
¯6 15 ¯3 12 0 9 3
3-⍨/1 9 2 8 3 7 4 6 5
¯8 ¯3 ¯7 ¯4 ¯6 ¯5 ¯5

Personally, I should have thought neg-n-wise reduction would have been
more usefully defined such that the equivalence does hold for all
functions rather than the conceptual oddity of the reversal of sub-
arrays.

btw. The number to the left is not, strictly speaking, another operand
to reduction but the left argument to the derived function (f/).

Veli-Matti

unread,
Dec 12, 2007, 10:45:35 AM12/12/07
to

> btw. The number to the left is not, strictly speaking, another operand
> to reduction but the left argument to the derived function (f/).

Touché!
-wm

0 new messages