Conversation started May 5, 2020 at 14:30.
May 5, 2020 2:30 PM
Welcome to APL Cultivation.
First thing first: Today's subject.
We've had a suggestion for looking closer at what you can do with (Domino: matrix division/inversion).
I also put together a text-justifying function last night that might be interesting to look at.
@all any opinions?
Happy to do this
RGS
RGS
I guess looking at ⌹ looks like a more standard APL Cultivation; looking at the text-justifying function would be a more exotic lesson... I'm happy with any/both!
@JamesHeslip Define "this".
id be interested in domino but either is fine.
Ah, I meant domino. Does the text-justification use domino?
May 5, 2020 2:34 PM
No, sorry. Let's do domino then.
The name is of course due to its symbol () which isn't really a domino (🁫) but rather a division sign in a quad, the latter representing division/inversion (÷).
You're of course familiar with the ÷ primitive.
Know that matrix multiplication is +.× but we don't have a corresponding operator for matrix division.
You can actually use +.×⍣¯1 for matrix division, but since wasn't always around (and certainly not ⍣¯1) and for notational ease, provides this functionality too.
Matrix inversion, what is that? Anyone volunteer a defintion?
RGS
RGS
@Adám say A is a matrix. Its inverse inv(A) is a matrix such that A×inv(A) = inv(A)×A = Id
M×inv(M)=I
Yup.
RGS
RGS
(assuming A is square)
May 5, 2020 2:41 PM
So, for now, let's keep to easy-to-remember matrices at hand:
      ⎕←E←2 2⍴2 7 1 8
2 7
1 8
      ⎕←P←2 2⍴3 1 4 1
3 1
4 1
Now if we invert P we get:
      ⌹P
¯1  1
 4 ¯3
And indeed:
      P+.×⌹P
1 0
0 1
Matrix division as a notation isn't usually used by TMNists (can I say that?), instead opting for multiplication by an inverse.
TMNists?
Traditional Mathematical Notationists
Ah, right.
The analogy with × and ÷ is pretty obvious, so APL defines A⌹B as (⌹B)+.×A just like a÷b is (÷b)×a
(Remember that matrix multiplication isn't commutative!)
      E⌹P
¯1 1
 5 4
      (⌹P)+.×E
¯1 1
 5 4
So far, there's nothing much controversial here.
However, isn't just for matrices. You can use it on vectors too, or even on a matrix and a vector.
     2 7⌹3 1
1.3
@all What does this ↑ mean?
RGS
RGS
(⌹3 1)+.×2 7
but why ⌹3 1 gives 0.3 0.1 is completely unknown to me...
May 5, 2020 2:52 PM
Good. Let's explore that.
     3 1+.×⌹3 1
1
Makes sense?
RGS
RGS
⌹v is the vector divided by the square of its norm!
See? I knew it'd be good to have a mathmagician here with us today.
We can even use on scalars, where it behaves just as ÷ except it errors on 0÷0 (where ÷ doesn't error). This is convenient if you want to make sure to catch division-by-zero errors.
For a vector v, does ((⌹v)+.×v)=(v+.×⌹v)?
ooh, so 2 7⌹3 1 is the "length" of the component of 2 7 in the 3 1-direction?
@JeffZeitlin Not just for vectors, even for square matrices. (Also, you mean not =)
@VladimirSotirov Yes. Well spotted. And this kind of leads us towards some of the tricks can do.
A common usage for is to solve equation systems. Consider (in TMN):
2x + 7y = 12
 x + 8y = 15
We can represent this as a matrix (our E) on the left of the equal signs and as a vector (12 15) on the right.
      12 15⌹E
¯1 2
This says x←¯1 and y←2. Let's check the result:
      2 7+.ׯ1 2
12
      1 8+.ׯ1 2
15
Yup.
May 5, 2020 3:03 PM
I'm not sure I follow exactly what was meant by @VladimirSotirov comment 'the "length" of the component of 2 7 in the 3 1-direction'
RGS
RGS
@JamesHeslip are you familiar with the dot product?
And if I do the "check" with the matrix, I get exactly the same results:
@JamesHeslip Think of 2 7 as a vector in the mathematical sense (not just APL list): it goes N-NE
(2 2 ⍴ 2 7 1 8) +.× ¯1 2
12 15
@RGS In terms of +.×?
May 5, 2020 3:05 PM
@JeffZeitlin Yup, of course. All good, isn't it? :-)
@Adám I drew a picture in paint of exactly that, but the "3 1-direction"?
@JamesHeslip - Think of graph paper.
A projection
You have a vector that goes from the origin, to 2,7
May 5, 2020 3:06 PM
Did Dyalog make that?
No, although it should be fairly easy to.
(Oops, I even had a typo in it — fixed now.)
I think I'm missing the point. How does that yield 1.3?
(Sorry, it feels like a rookie question)
@JamesHeslip What is the length of (2,7)?
@JamesHeslip draw the perpendicular segment from the point 2 7 to the line through 0 0 and 3 1. The intersection point has coordinates (2 7⌹3 1)×⌹3 1: ⌹3 1 is the unit vector in the same direction as 3 1; the distance from 0 0 to the point is |2 7⌹3 1.
I would go with ((2*2)+(7*2))*0.5
7.280109889280518
May 5, 2020 3:14 PM
@JamesHeslip Right.
@VladimirSotirov Hold on, are you sure that's right? What notation are you using?
RGS
RGS
@VladimirSotirov ⌹v is not a unit vector (in general)
The unit vector in the direction of 3 1 is 3 1÷(+/3 1*2)*0.5
wait, so (⌹v)≡ v÷+.×⍨v for vectors?
RGS
RGS
@VladimirSotirov looks like so
ok, cool, thanks! so then the coordinates of the intersection point are (2 7⌹3 1)×3 1, so the "length" in the 3 1-direction is what scalar multiple of 3 1 gives those coordinates?
RGS
RGS
May 5, 2020 3:24 PM
@VladimirSotirov (+1)
@JamesHeslip Sorry for the delay. Here ↑ is (2 7⌹3 1)×3 1 which gives 3.9 1.3 with the original vectors.
So you can see that (0 0) (3.9 1.3) (2.7) forms a right angle.
RGS
RGS
so to understand v⌹w we need to think of the vector u, which is the shadow that v casts over w. The shadow is (v⌹w) times longer than w :)
in the image, the shadow cast is the (3.9, 1.3)
In other words, if we project 2 7 perpendicularly to the extension of 3 1 we hit a point on 3 1's extension which is 1.3×3 1 from 0 0.
I just poked my terp with what I thought should be a related concept, and got results that I don't understand...
Why don't I get 3.9j1.3 if I divide 2j7 by 3j1?
Another way to look at it is that 2 7⌹3 1 is the factor you need to multiply 3 1 with to get closest to 2 7.
May 5, 2020 3:33 PM
@Adám Ahh, that makes so much sense. Thank you.
RGS
RGS
@Adám (+1)
@JeffZeitlin complex number division is properly defined and doesn't really match what we are doing with 2-element vectors if I understand correctly
(For the uninitiated, regarding Jeff's question: AjB means A+B×i)
RGS
RGS
just think about the fact that multiplying two complex numbers has little to do with the "corresponding" 2D vectors in terms of vector operations we are used to
(if I'm not missing anything; pls correct me)
OK, I'll accept that; I thought there would be a relationship because basic instruction (elementary school level) always seems to discuss complex numbers as though they were vectors on the complex plane...
@JeffZeitlin if you identify complex numbers with vectors, 2j7÷3j1 is the complex number which when scaled up by the norm of3j1 and rotated by the angle 3j1 makes with the x-axis lands you on 2j7
@JeffZeitlin whereas multilpying real numbers together (or real number by vector) corresponds to sclaing, multiplying complex numbers together in general corresponds to scaling by the magnitude and rotating by the angle from the x-axis
RGS
RGS
May 5, 2020 3:38 PM
@JeffZeitlin we do discuss complex numbers like that, but if you read Vladimir's remark, describing these ^ operations in terms of dot products isn't straightforward
Got it. So when we're playing with two-component vectors here, we're NOT rotating through an angle.
RGS
RGS
and would probably need some extra trig :)
Phew. Skilled crowd today.
OK, let's see if we can get through some more stuff
So this was actually interesting: in a sense finds the "closest" value.
In fact, when we used it to solve the equation system, it also found the "closest values", which happened to be spot on.
RGS
RGS
@Adám does it perform least squares then? when the system can't be solved exactly
It does. That was my next example.
RGS
RGS
May 5, 2020 3:43 PM
sorry :'(
But please show it
OK, remember how we found x y≡¯1 2 with 12 15⌹E ?
So clearly, if we add x and y we should get 1:
      12 15 1⌹E⍪1 1
¯1 2
Yes, it still holds, as this means:
2x 7y=12
 x 8y=15
 x  y= 1
But what if we tell APL that the last sum doesn't equal 1?
RGS
RGS
(then you are a bad person!)
      12 15 1.1⌹E⍪1 1
¯0.94129 1.98903
:-D What non-sense is this? It doesn't even fulfil any of the equations:
      2 7+.×x y
12.0406
      1 8+.×x y
14.971
      1 1+.×x y
1.04774
But as you can see, it is pretty close.
This is an over-determined system, so APL found the solution that fits best.
It defines "best" by a very common method called the least squares fit, which can also be used to make other kinds of fits.
What it means is that it tries to minimise the squares of the "errors". In a sense, it smoothes the errors out, which means we can use it for smooth curve-fitting too.
Unfortunately, we won't have enough time to go through many possibilities today, but you can see a few uses if you search APLcart for ⌹ fit. Let's just take the very first one from there: ⊢⌹1,∘⍪⊣
Let's say e.g.
x←0 1 3 4 5
y←0 2 4 7 7
(Yes, this was drawn by APL)
      x(⊢⌹1,∘⍪⊣)y
0.22093 1.45349
This means the best linear fit is y(x)=0.22093x+1.45349
@all I guess we have to stop here. Any questions?
May 5, 2020 4:01 PM
so 12 15 1.1⌹E⍪1 1 gives x y such that (E⍪1 1)+.× x y is closest to 12 15 1.1?
Yes, that's correct.
cool; thank you! this was very illuminating.
I'm happy you enjoyed it. I'm sorry my linear algebra-fu wasn't as quick at hand as I could have wished for.
Thank you all for participating so engaged!
 
Conversation ended May 5, 2020 at 16:04.