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

clarifying scalar/vector/matrix/??? extension

107 views
Skip to first unread message

luserdroog

unread,
May 4, 2014, 2:53:49 AM5/4/14
to
I finally got to the stage in translating my teapot drawing
program where it needs to do matrix multiplcations to
convert the Bezier matrix into a bicubic polynomial coefficient
matrix. After some false starts with recursive user-functions,
I realized it should be very simple with APL (and thus inca),
but I'm fuzzy on some of the details (and thus inca doesn't
implement it correctly).

So I'm going to go through my understanding of how the basic
functions apply to scalars/.../matrices on the left and right,
and please correct me if (where) I err.

scalar/scalar -> scalar
z < x.y straight integer/integer function (in inca)

scalar/vector -> vector
vector/scalar -> vector
z_i < a.w_i
z_i < a_i.w apply the scalar to each element of vector

scalar/matrix -> matrix
matrix/scalar -> matrix
--same as vector, using raveled representation of matrix

vector/vector -> vector
assume(insist) dimensions are equal
and apply the function upon corresponding elements

vector/matrix -> matrix
length of vector must match columns of matrix
decompose the matrix into rows and
recurse to the vector/vector case for each vector/row pair
concatenating the result into the original matrix shape

matrix/vector -> ???
Nars2000 gives me "RANK ERROR" for (3 3⍴⍳9)×2 3 4.
but shouldn't this be ok if the vector can enter
as a column matrix with appropriate dimensions for
a multiply?

Once this much is cleared up, then I can tackle the
reduce operator, which is all messed up wrt matrices.

lok...@gmail.com

unread,
May 4, 2014, 3:24:30 AM5/4/14
to
On Sunday, 4 May 2014 14:53:49 UTC+8, luserdroog wrote:

> matrix/vector -> ???
> Nars2000 gives me "RANK ERROR" for (3 3⍴⍳9)×2 3 4.
> but shouldn't this be ok if the vector can enter
> as a column matrix with appropriate dimensions for
> a multiply?

I think what you are looking for is the ⍤ operator:

(⍳3) ×⍤1 (3 3⍴⍳9)
1 4 9
4 10 18
7 16 27

Regards,
Elias

luserdroog

unread,
May 4, 2014, 3:30:41 AM5/4/14
to
On Sunday, May 4, 2014 1:53:49 AM UTC-5, luserdroog wrote:

>
> Once this much is cleared up, then I can tackle the
> reduce operator, which is all messed up wrt matrices.

Oh, right, and then there's this other case:

matrix/matrix -> matrix
same as raveled vectors of everything right?
and it's the general matrix multiply (dot operator)
that really handles the row/column/k-iota mashup, right?

But if general matrix multiple (dot) is
left-f reduce A right-f transpose B,
(like I have implemented),
then there's nothing to sum over.
I've lost k, somehow, I think.


luserdroog

unread,
May 4, 2014, 3:35:32 AM5/4/14
to
Hm. I see. I tried reading the paper on that one,
Functions and Operators, IIRC, but I didn't not
understand it. But still the vector comes in from
the left here, not the right. So it is acceptable
(non-surprising) behavior to reject matrix/vector
as a type combination?

luserdroog

unread,
May 4, 2014, 4:29:22 AM5/4/14
to
Well, now I'm thoroughly confused!
NARS2000 rejects all the cases I've been struggling
to interpret sensibly.

(3 3⍴⍳9)×(3 1⍴⍳3)
LENGTH ERROR
(3 3⍴⍳9)×(3 1⍴⍳3)

(3 3⍴⍳9)×(1 3⍴⍳3)
LENGTH ERROR
(3 3⍴⍳9)×(1 3⍴⍳3)

(1 3⍴⍳3)×(3 3⍴⍳9)
LENGTH ERROR
(1 3⍴⍳3)×(3 3⍴⍳9)

(3 1⍴⍳3)×(3 3⍴⍳9)
LENGTH ERROR
(3 1⍴⍳3)×(3 3⍴⍳9)

(9 1⍴⍳3)×(3 3⍴⍳9)
LENGTH ERROR
(9 1⍴⍳3)×(3 3⍴⍳9)

(⍳3)×3 3⍴⍳9
RANK ERROR
(⍳3)×3 3⍴⍳9

(3 3⍴⍳9)×⍳3
RANK ERROR
(3 3⍴⍳9)×⍳3

Bob Smith

unread,
May 4, 2014, 8:44:44 AM5/4/14
to
Try one of these

(3 3⍴⍳9)×[1] ⍳3
1 2 3
8 10 12
21 24 27
(3 3⍴⍳9)×[2] ⍳3
1 4 9
4 10 18
7 16 27
(3 3⍴⍳9)×⍤1 ⍳3
1 4 9
4 10 18
7 16 27

--
_________________________________________
Bob Smith -- bsm...@sudleydeplacespam.com
http://www.sudleyplace.com - http://www.nars2000.org

To reply to me directly, delete "despam".

James J. Weinkam

unread,
May 7, 2014, 4:22:03 PM5/7/14
to
luserdroog wrote:
>
> (3 3⍴⍳9)×(3 1⍴⍳3)
> LENGTH ERROR

You are trying to multiply a 3x3 array and a 3X1 array. These are simply not conformable with respect to
multiplication. However, inner and outer product are possible.

If you change the right argument to a 3 element vector then simple multiplication gives a RANK error; but inner product,
outer product, and multiply with axis are all possible.

luserdroog

unread,
May 8, 2014, 3:06:23 AM5/8/14
to
Rats. That means my whole semantics for inca is very wrong.
I got jot-dot to work (with vectors) by simply transposing
'a' into a column vector and 'w' into a row vector. All the
work is done in a huge recursive macro used by all the
basic functions.

./inca
(~3)@.=~3
0:3 3
1 0 0
0 1 0
0 0 1
(\@~3)=\@\@~3
0:3 3
1 0 0
0 1 0
0 0 1

So this is good news, in a way. I should be able to make
the basic functions much simpler by insisting that
arguments match in rank and dimension. But now I need
an error mechanism, and I'm tempted to use setjmp/longjmp
even though it was a chore to remove them from xpost
for greater portability.

But then I'll need more machinery for operators, too,
I think. I'll need a closure type to describe the
resulting "function" expressed by an operator, so it
too may be composed by another operator. And if possible,
user functions should be composable with operators.
But I'm not entirely sure how to implement that.
It may be easier to envisage with the closure type written.

luserdroog

unread,
May 10, 2014, 2:49:55 AM5/10/14
to
Alright. I think I've got a better handle on matrix
multiplication now. At the risk of heresy, I think
it's not such a problem to have defined results
where other APLs yield an error. The reverse is more
likely to be problematical.

But I still need to fix my dot operator to work on
matrices. For this I think I've found two different
approaches. The textbook definition of the matrix
multiply is "f reduce row_i_of_a g column_j_of_w".
And this is applied for each (i,j) pair in the
resulting matrix. An example (ASCII art).

1 2 3 1 2 3
4 5 6 +.× 4 5 6
7 8 9 7 8 9

+/1 2 3×1 +/1 2 3×2 +/1 2 3×3
4 5 6
7 8 9

+/4 5 6×1 +/4 5 6×2 +/4 5 6×3
4 5 6
7 8 9

+/7 8 9×1 +/7 8 9×2 +/7 8 9×3
4 5 6
7 8 9

=>
30 36 42
66 81 96
102 126 150

But there's another way that I think could work.
If I transform a and w into 3x3x3 arrays, then reduce
should collapse the result back down to 3x3.

+/ 1 2 3 1 1 1
1 2 3 4 4 4
1 2 3 7 7 7

4 5 6 2 2 2
4 5 6 × 5 5 5
4 5 6 8 8 8

7 8 9 3 3 3
7 8 9 6 6 6
7 8 9 9 9 9

=>

+/ 1 2 3
4 8 12
7 14 21

8 10 12
20 25 30
32 40 48

21 24 27
42 48 54
63 72 81

=>

30 36 42
66 81 96
102 126 150

And this may be more efficient in inca by allocating fewer
temporary arrays.

luserdroog

unread,
May 10, 2014, 3:07:07 AM5/10/14
to
Still another way. Using a 3×3x3, but merely duplicating
the transpose of w for each "row matrix" of a. Then we need to
reduce over columns, and reshape the resulting column vector.

+//1 2 3 1 4 7
1 2 3 2 5 8
1 2 3 3 6 9
4 5 6 1 4 7
4 5 6 × 2 5 8
4 5 6 3 6 9
7 8 9 1 4 7
7 8 9 2 5 8
7 8 9 3 6 9

=>

+// 1 8 21
2 10 24
3 12 27
4 20 42
8 25 48
12 30 54
7 32 63
14 40 72
21 48 81

=>

30
36
42
66
81
96
102
126
150

This appears to work, but doesn't seem to offer any
implementation benefit. Since I don't have column reduction,
I'd have to do a transpose sandwich.

luserdroog

unread,
May 11, 2014, 1:40:09 AM5/11/14
to
On Saturday, May 10, 2014 2:07:07 AM UTC-5, luserdroog wrote:
> On Saturday, May 10, 2014 1:49:55 AM UTC-5, luserdroog wrote:
>
>
> > 1 2 3 1 2 3
> > 4 5 6 +.× 4 5 6
> > 7 8 9 7 8 9
>
[snip]
>
> > 30 36 42
> > 66 81 96
> > 102 126 150
> >
[snip]
Haha! It works. The implementation benefit is that everything
stays 2D. It does involve a bit of transposing in there, but
not as much as I feared. Nothing actually needed a sandwich.

./inca
a+..a<3 3#1+~9
0:3 3
30 36 42
66 81 96
102 126 150
(3 4#1+~12)+..(4 3#1+~12)
0:3 3
70 80 90
158 184 210
246 288 330

0 new messages