From 94a7a4c33fe67c0a9cc3f8ce210d4632fef2d918 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Sun, 6 Aug 2023 17:22:07 +1000 Subject: [PATCH] ivy: matrix divide, mdiv Dead easy now we have matrix invert. Moore-Penrose inverse will enable more uses; see issue #146 --- doc.go | 2 +- mobile/help.go | 2 +- parse/help.go | 3 ++- testdata/binary_matrix.ivy | 7 +++++++ value/binary.go | 12 ++++++++++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/doc.go b/doc.go index 1d6b400..e0db109 100644 --- a/doc.go +++ b/doc.go @@ -158,7 +158,7 @@ Binary operators In ivy: abs(A) gives count, A <= 0 inserts zero Index of A⍳B iota The location (index) of B in A; 1+⌈/⍳⍴A if not found In ivy: origin-1 if not found (i.e. 0 if one-indexed) - Matrix divide A⌹B Solution to system of linear equations Ax = B + Matrix divide A⌹B mdiv Solution to system of linear equations Ax = B Rotation A⌽B rot The elements of B are rotated A positions left Rotation A⊖B flip The elements of B are rotated A positions along the first axis Logarithm A⍟B log Logarithm of B to base A diff --git a/mobile/help.go b/mobile/help.go index 3a02866..e32f411 100644 --- a/mobile/help.go +++ b/mobile/help.go @@ -164,7 +164,7 @@ Compression A/B sel Select elements in B corresponding to ones In ivy: abs(A) gives count, A <= 0 inserts zero Index of A⍳B iota The location (index) of B in A; 1+⌈/⍳⍴A if not found In ivy: origin-1 if not found (i.e. 0 if one-indexed) -Matrix divide A⌹B Solution to system of linear equations Ax = B +Matrix divide A⌹B mdiv Solution to system of linear equations Ax = B Rotation A⌽B rot The elements of B are rotated A positions left Rotation A⊖B flip The elements of B are rotated A positions along the first axis Logarithm A⍟B log Logarithm of B to base A diff --git a/parse/help.go b/parse/help.go index 9d944a3..0a7766f 100644 --- a/parse/help.go +++ b/parse/help.go @@ -154,7 +154,7 @@ var helpLines = []string{ "\t In ivy: abs(A) gives count, A <= 0 inserts zero", "\tIndex of A⍳B iota The location (index) of B in A; 1+⌈/⍳⍴A if not found", "\t In ivy: origin-1 if not found (i.e. 0 if one-indexed)", - "\tMatrix divide A⌹B Solution to system of linear equations Ax = B", + "\tMatrix divide A⌹B mdiv Solution to system of linear equations Ax = B", "\tRotation A⌽B rot The elements of B are rotated A positions left", "\tRotation A⊖B flip The elements of B are rotated A positions along the first axis", "\tLogarithm A⍟B log Logarithm of B to base A", @@ -476,6 +476,7 @@ var helpBinary = map[string]helpIndexPair{ "fill": {145, 146}, "sel": {147, 148}, "iota": {149, 150}, + "mdiv": {151, 151}, "rot": {152, 152}, "flip": {153, 153}, "log": {154, 154}, diff --git a/testdata/binary_matrix.ivy b/testdata/binary_matrix.ivy index 51f4c27..106b712 100644 --- a/testdata/binary_matrix.ivy +++ b/testdata/binary_matrix.ivy @@ -132,6 +132,13 @@ 1 2 3 4 5 6 +(2 2 rho 1 2 3 4) mdiv (2 2 rho 5 6 7 8) + 5 4 + -4 -3 + +(5 8) mdiv (2 2 rho 1 2 2 -1) + 21/5 2/5 + (2 3 rho iota 10) ** 5 1 32 243 1024 3125 7776 diff --git a/value/binary.go b/value/binary.go index 6561af5..6763433 100644 --- a/value/binary.go +++ b/value/binary.go @@ -355,6 +355,18 @@ func init() { }, }, + { // Matrix division + name: "mdiv", + elementwise: false, + whichType: binaryArithType, + fn: [numType]binaryFn{ + // TODO: Other types? + matrixType: func(c Context, u, v Value) Value { + return innerProduct(c, v.(*Matrix).inverse(c), "+", "*", u) + }, + }, + }, + { // Euclidean integer division. name: "div", elementwise: true,