Discussion:
[Bug-apl] inner product
e***@gmx.com
2017-03-17 16:17:19 UTC
Permalink
Hi

what am i missing here?

a←b←5 3⍴⍳8

a=b

+/a=b

a+.=b length error
Christian Robert
2017-03-17 16:26:42 UTC
Permalink
a+.=⍉b
3 0 0 0 0
0 3 0 0 0
0 0 3 0 0
0 0 0 3 0
0 0 0 0 3
Post by e***@gmx.com
Hi
what am i missing here?
a←b←5 3⍴⍳8
a=b
+/a=b
a+.=b length error
e***@gmx.com
2017-03-17 17:14:46 UTC
Permalink
thanks

i have no clue how the result of a+.=⍉b as 5x5 result is obtained from a 5x3 array and 3x5 array shouldn't it be a 15x15 result ... but i guess i really just want to use +/a=b :)


On Fri, 17 Mar 2017 12:26:42 -0400
Post by Christian Robert
a+.=⍉b
3 0 0 0 0
0 3 0 0 0
0 0 3 0 0
0 0 0 3 0
0 0 0 0 3
Post by e***@gmx.com
Hi
what am i missing here?
a←b←5 3⍴⍳8
a=b
+/a=b
a+.=b length error
Juergen Sauermann
2017-03-17 17:44:24 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi,<br>
<br>
that would be more like an outer product:<br>
<br>
</font><font face="Courier New, Courier, monospace"><b>      a←b←5
3⍴⍳8</b><b><br>
</b><b>      ⍴a∘.=b</b><b><br>
</b><b>5 3 5 3</b></font><br>
<br>
The inner product reduces away the two middle axes of the outer
product.<br>
<br>
In the <b>⍉b</b> case you have<br>
<br>
<br>
<font face="Courier New, Courier, monospace"><b>      ⍴a∘.=⍉b</b><b><br>
</b><b>5 3 3 5</b><b><br>
</b><b><br>
</b><b>      ⍴a+.=⍉b</b><b><br>
</b><b>5 5</b></font><br>
<br>
That is, BTW, the reason while the last axis of a and the first axis
of b must match; otherwise<br>
the right function of . would get a length error.<br>
<br>
/// Jürgen<br>
<br>
<br>
<br>
<div class="moz-cite-prefix">On 03/17/2017 06:14 PM, <a class="moz-txt-link-abbreviated" href="mailto:***@gmx.com">***@gmx.com</a>
wrote:<br>
</div>
<blockquote cite="mid:***@gmx.com"
type="cite">
<pre wrap="">thanks

i have no clue how the result of a+.=⍉b as 5x5 result is obtained from a 5x3 array and 3x5 array shouldn't it be a 15x15 result ... but i guess i really just want to use +/a=b :)


On Fri, 17 Mar 2017 12:26:42 -0400
Christian Robert <a class="moz-txt-link-rfc2396E" href="mailto:***@polymtl.ca">&lt;***@polymtl.ca&gt;</a> wrote:

</pre>
<blockquote type="cite">
<pre wrap=""> a+.=⍉b
3 0 0 0 0
0 3 0 0 0
0 0 3 0 0
0 0 0 3 0
0 0 0 0 3



On 2017-03-17 12:17, <a class="moz-txt-link-abbreviated" href="mailto:***@gmx.com">***@gmx.com</a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Hi

what am i missing here?

a←b←5 3⍴⍳8

a=b

+/a=b

a+.=b length error


</pre>
</blockquote>
<pre wrap="">
</pre>
</blockquote>
<pre wrap="">

</pre>
</blockquote>
<br>
</body>
</html>
Juergen Sauermann
2017-03-17 16:29:24 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi,<br>
<br>
the number of columns in <b>a</b> must match the number of rows
in <b>b</b> (or the other way around,<br>
I don't quite remember).<br>
</font><br>
Try:<br>
<br>
<font face="Courier New, Courier, monospace"><b>      a+.=⍉b</b><b><br>
</b><b>3 0 0 0 0</b><b><br>
</b><b>0 3 0 0 0</b><b><br>
</b><b>0 0 3 0 0</b><b><br>
</b><b>0 0 0 3 0</b><b><br>
</b><b>0 0 0 0 3</b></font><br>
<br>
/// Jürgen<br>
<br>
<br>
<div class="moz-cite-prefix">On 03/17/2017 05:17 PM, <a class="moz-txt-link-abbreviated" href="mailto:***@gmx.com">***@gmx.com</a>
wrote:<br>
</div>
<blockquote cite="mid:***@gmx.com"
type="cite">
<pre wrap="">Hi

what am i missing here?

a←b←5 3⍴⍳8

a=b

+/a=b

a+.=b length error



</pre>
</blockquote>
<br>
</body>
</html>
e***@gmx.com
2017-03-17 17:05:09 UTC
Permalink
yes last dimension of a must be = to first dimension of b

but then

x←1 2 3 4
y←1 3 3 3

x+.=y works


On Fri, 17 Mar 2017 17:29:24 +0100
Hi,
the number of columns in a must match the number of rows in b (or the other way around,
I don't quite remember).
      a+.=⍉b
3 0 0 0 0
0 3 0 0 0
0 0 3 0 0
0 0 0 3 0
0 0 0 0 3
/// Jürgen
Hi
what am i missing here?
a←b←5 3⍴⍳8
a=b
+/a=b
a+.=b length error
Juergen Sauermann
2017-03-17 17:18:58 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi,<br>
<br>
yes. Because the first dimension of <b>a</b> is 4 and the last
dimension of <b>b</b> is also 4.<br>
<br>
/// Jürgen<br>
<br>
</font><br>
<div class="moz-cite-prefix">On 03/17/2017 06:05 PM, <a class="moz-txt-link-abbreviated" href="mailto:***@gmx.com">***@gmx.com</a>
wrote:<br>
</div>
<blockquote cite="mid:***@gmx.com"
type="cite">
<pre wrap="">
yes last dimension of a must be = to first dimension of b

but then

x←1 2 3 4
y←1 3 3 3

x+.=y works


On Fri, 17 Mar 2017 17:29:24 +0100
Juergen Sauermann <a class="moz-txt-link-rfc2396E" href="mailto:***@t-online.de">&lt;***@t-online.de&gt;</a> wrote:

</pre>
<blockquote type="cite">
<pre wrap="">Hi,

the number of columns in a must match the number of rows in b (or the other way around,
I don't quite remember).

Try:

      a+.=⍉b
3 0 0 0 0
0 3 0 0 0
0 0 3 0 0
0 0 0 3 0
0 0 0 0 3

/// Jürgen


On 03/17/2017 05:17 PM, <a class="moz-txt-link-abbreviated" href="mailto:***@gmx.com">***@gmx.com</a> wrote:
Hi

what am i missing here?

a←b←5 3⍴⍳8

a=b

+/a=b

a+.=b length error




</pre>
</blockquote>
<pre wrap="">

</pre>
</blockquote>
<br>
</body>
</html>
e***@gmx.com
2017-03-17 19:00:10 UTC
Permalink
Hi

I assume you mean the vectors both have same length as their single dimension (no first or last - the same)

making them into 1 4⍴ arrays then requires the ⍉ use

don't you treat vectors differntly in the gnuapl code from 'true arrays' with 2 (or more dimensions)?


On Fri, 17 Mar 2017 18:18:58 +0100
Hi,
yes. Because the first dimension of a is 4 and the last dimension of b is also 4.
/// Jürgen
yes last dimension of a must be = to first dimension of b
but then
x←1 2 3 4
y←1 3 3 3
x+.=y works
On Fri, 17 Mar 2017 17:29:24 +0100
Hi,
the number of columns in a must match the number of rows in b (or the other way around,
I don't quite remember).
      a+.=⍉b
3 0 0 0 0
0 3 0 0 0
0 0 3 0 0
0 0 0 3 0
0 0 0 0 3
/// Jürgen
Hi
what am i missing here?
a←b←5 3⍴⍳8
a=b
+/a=b
a+.=b length error
Nick Lobachevsky
2017-03-17 21:01:26 UTC
Permalink
The key to understanding inner product is that the inner dimensions of
the arguments have to be the same. The inner dimension here is 3.

a←2 3⍴⍳6
b←3 4⍴⍳12
a
0 1 2
3 4 5
b
0 1 2 3
4 5 6 7
8 9 10 11
a+.×b
20 23 26 29
56 68 80 92

To solve this, first transpose the right argument such that the inner
dimension goes to the back of the array and both arguments have the
same number of columns.

(¯1⌽⍳⍴⍴b)⍉b
0 4 8
1 5 9
2 6 10
3 7 11
a
0 1 2
3 4 5

Do the operations for every combination of rows in a and ⍉b. As we
are doing +.×
0 1 2 × 0 4 8 is 0 4 16, +/0 4 16 is 20. First element of the result
0 1 2 × 1 5 9 is 0 5 18, +/0 5 18 is 23. Second element of the result
And so on. Loop until done.

Shape of the result is (¯1↓⍴a),1↓⍴b or 2 4

For the vector and vector case, the lengths of both vectors have to be
the same. The result is simply +/ a × b

For higher order matrices, as before, the inner dimensions are
important. The others less so.
a←2 5 1 3⍴⍳30
b←3 4 2⍴⍳12
Here the idea is to collapse (i.e. multiply together) all but the
inner dimensions, then compute the result as if both arguments were
two dimensional matrices.
a←10 3⍴⍳30
b←3 8⍴⍳24
And as before, the shape of the product is (¯1↓⍴a),1↓⍴b or 2 5 1 4 2

See also http://www.dyalog.com/uploads/conference/dyalog16/presentations/U08_SIMD_Boolean_Array_Algorithms_slides.pdf
(The part about the STAR Inner Product Algorithm)

and

http://www.jsoftware.com/papers/innerproduct/ip1.htm
e***@gmx.com
2017-03-17 22:28:06 UTC
Permalink
very nice work - thanks you for spending the time .. everthing here holds when replacing +.x with +.= (which is of more interest to me)

On Fri, 17 Mar 2017 22:01:26 +0100
Post by Nick Lobachevsky
The key to understanding inner product is that the inner dimensions of
the arguments have to be the same. The inner dimension here is 3.
a←2 3⍴⍳6
b←3 4⍴⍳12
a
0 1 2
3 4 5
b
0 1 2 3
4 5 6 7
8 9 10 11
a+.×b
20 23 26 29
56 68 80 92
To solve this, first transpose the right argument such that the inner
dimension goes to the back of the array and both arguments have the
same number of columns.
(¯1⌽⍳⍴⍴b)⍉b
0 4 8
1 5 9
2 6 10
3 7 11
a
0 1 2
3 4 5
Do the operations for every combination of rows in a and ⍉b. As we
are doing +.×
0 1 2 × 0 4 8 is 0 4 16, +/0 4 16 is 20. First element of the result
0 1 2 × 1 5 9 is 0 5 18, +/0 5 18 is 23. Second element of the result
And so on. Loop until done.
Shape of the result is (¯1↓⍴a),1↓⍴b or 2 4
For the vector and vector case, the lengths of both vectors have to be
the same. The result is simply +/ a × b
For higher order matrices, as before, the inner dimensions are
important. The others less so.
a←2 5 1 3⍴⍳30
b←3 4 2⍴⍳12
Here the idea is to collapse (i.e. multiply together) all but the
inner dimensions, then compute the result as if both arguments were
two dimensional matrices.
a←10 3⍴⍳30
b←3 8⍴⍳24
And as before, the shape of the product is (¯1↓⍴a),1↓⍴b or 2 5 1 4 2
See also http://www.dyalog.com/uploads/conference/dyalog16/presentations/U08_SIMD_Boolean_Array_Algorithms_slides.pdf
(The part about the STAR Inner Product Algorithm)
and
http://www.jsoftware.com/papers/innerproduct/ip1.htm
Nick Lobachevsky
2017-03-18 06:57:49 UTC
Permalink
Thank you.

Forgot to mention scalar extension. When a scalar is supplied as an
argument, it's lengthened to match the inner dimension of the other
argument.
(3 4⍴'cattrattfatt') +.= 't'
2 2 2
(3 4⍴'cattrattfatt') +.= 'tttt'
2 2 2
'a' +.= 3 4⍴'catratfatbat'
1 1 1 1
'aaa' +.= 3 4⍴'catratfatbat'
1 1 1 1
If both arguments are scalar (a silly case) then it would be same as
if only the second function was used.
2+.×6
12

Loading...