Zulip Chat Archive

Stream: new members

Topic: Matrix operations


li sheng (Jun 15 2021 at 16:30):

I am new to lean .Who can tell me an example , in which we input two matrices and multiply them using standard library as in python ? A=Matrix([[1,2],[3,4]]),B=Matrix ([[1,1],[0,0]]),A*B=? in lean .Thanks !

li sheng (Jun 15 2021 at 16:41):

Is there anyone who will give me an example of matrix multiplication ? Input two specific matrices and multiply them. Thanks!

Eric Wieser (Jun 15 2021 at 16:43):

docs#matrix.mul

Eric Wieser (Jun 15 2021 at 16:43):

Aka M ⬝ N

li sheng (Jun 15 2021 at 16:44):

Is there anyone who will give me an example of matrix multiplication ? Input two specific matrices and multiply them. Thanks!

Eric Wieser (Jun 15 2021 at 16:47):

If you're brand new to lean that might not help all that much - if so, can you elaborate on your question a little?

Eric Wieser (Jun 15 2021 at 16:48):

Oh i see there's a copy of this thread in #new members - can an admin move this one?

Notification Bot (Jun 15 2021 at 16:48):

This topic was moved here from #general > Matrix operations by Bryan Gin-ge Chen

Anne Baanen (Jun 15 2021 at 17:02):

The matrix library is not optimized for these purposes, but you can do a bit of computing with them:

import data.matrix.notation
open_locale matrix

-- There's no `repr` defined for matrices, so the following line outputs a big pile of unreadable syntax.
-- #eval ((![![1, 2], ![3, 4]] ⬝ ![![1, -3], ![-2, 4]]) : matrix _ _ ℤ)

#eval ((![![1, 2], ![3, 4]]  ![![1, -3], ![-2, 4]]) 0 0 : ) -- -3
#eval ((![![1, 2], ![3, 4]]  ![![1, -3], ![-2, 4]]) 0 1 : ) -- 5
#eval ((![![1, 2], ![3, 4]]  ![![1, -3], ![-2, 4]]) 1 0 : ) -- -5
#eval ((![![1, 2], ![3, 4]]  ![![1, -3], ![-2, 4]]) 1 1 : ) -- 7

Anne Baanen (Jun 15 2021 at 17:07):

You can fix the "no has_repr" issue as follows:

import data.matrix.notation
open_locale matrix

instance [has_repr α] : has_repr (fin n  α) :=
{ repr := λ f, "![" ++ (string.intercalate ", " ((list.fin_range n).map (λ n, repr (f n)))) ++ "]" }

instance [has_repr α] : has_repr (matrix (fin m) (fin n) α) :=
(infer_instance : has_repr (fin m  fin n  α))

#eval ((![![1, 2], ![3, 4]]  ![![1, -3], ![-2, 4]]) : matrix _ _ ) -- ![![-3, 5], ![-5, 7]]

#eval ((![![1, 2], ![3, 4]]  ![![1, -3], ![-2, 4]]) 0 0 : ) -- -3
#eval ((![![1, 2], ![3, 4]]  ![![1, -3], ![-2, 4]]) 0 1 : ) -- 5
#eval ((![![1, 2], ![3, 4]]  ![![1, -3], ![-2, 4]]) 1 0 : ) -- -5
#eval ((![![1, 2], ![3, 4]]  ![![1, -3], ![-2, 4]]) 1 1 : ) -- 7

li sheng (Jun 15 2021 at 17:09):

This looks ugly! I think there should have a better one in lean.

Eric Wieser (Jun 15 2021 at 17:09):

Can we add those has_repr to data/matrix/notation.lean?

Anne Baanen (Jun 15 2021 at 17:11):

li sheng said:

This looks ugly! I think there should have a better one in lean.

Feel free to implement your own if you think it's ugly! Just replace the (infer_instance : has_repr (fin m → fin n → α)) line with { repr := λ M, your_string_goes_here } and have fun :)

Anne Baanen (Jun 15 2021 at 17:11):

Eric Wieser said:

Can we add those has_repr to data/matrix/notation.lean?

Sure, I'll make a PR after dinner!

Eric Wieser (Jun 15 2021 at 17:13):

!![a, b; c, d] might be a slightly less ugly notation for matrices, inspired by Matlab. I don't know how hard it is to tell lean how to use that.

li sheng (Jun 15 2021 at 17:17):

@Anne Baanen Thank you for your solution,I will try it and find other solutions!

Eric Wieser (Jun 15 2021 at 17:25):

@li sheng, what specifically is it you find ugly?

Eric Wieser (Jun 15 2021 at 17:26):

What would you like to be able to write instead?

li sheng (Jun 15 2021 at 17:36):

I want to use docs#matrix.mul! !![1,2;3,4] looks nice !

Johan Commelin (Jun 15 2021 at 17:39):

The snippets above use matrix.mul already.

li sheng (Jun 15 2021 at 17:42):

docs#matrix.mul provides us with plenty of functions,we can "encapsulate" them for convenient computations.

Yakov Pechersky (Jun 15 2021 at 17:42):

The nice thing about the current matrix notation is that it supports 1, 2, 3,... dimensional "matrices".

Yakov Pechersky (Jun 15 2021 at 17:43):

Semicolons limit you to 2D

Eric Wieser (Jun 15 2021 at 18:13):

I suggested !! because it doesn't rule out the existing notation

Anne Baanen (Jun 15 2021 at 18:49):

Opened #7953.

Patrick Massot (Jun 15 2021 at 19:23):

li sheng said:

I am new to lean .Who can tell me an example , in which we input two matrices and multiply them using standard library as in python ? A=Matrix([[1,2],[3,4]]),B=Matrix ([[1,1],[0,0]]),A*B=? in lean .Thanks !

I'm afraid you'll be very disappointed by Lean if this is your first question. In an ideal world, a proof assistant would be able to do everything a computer algebra system such as Sage is doing and then add proofs on top of that. Unfortunately we are very far away from this. Up to now these are almost disjoint worlds. Use Sage for computations and Lean for proofs.

Kevin Buzzard (Jun 15 2021 at 22:10):

Yes -- Lean is not a good "computer" at the minute -- it is designed to prove theorems, not do computations.

Eric Wieser (Jun 10 2022 at 09:51):

Eric Wieser said:

!![a, b; c, d] might be a slightly less ugly notation for matrices, inspired by Matlab. I don't know how hard it is to tell lean how to use that.

I implemented !ₘ[1, 2; 3, 4] in #14665

Eric Wieser (Jul 05 2022 at 16:08):

This PR has now split into #14991 and #14992, which respectively add !![a, b; c, d] notation (without the type cast) and type casting as matrix.of ![![a, b], ![b, c]] (without the !! notation). I'll fix the conflicts in whichever one doesn't go in first.


Last updated: Dec 20 2023 at 11:08 UTC