Zulip Chat Archive

Stream: new members

Topic: Concatenate matrices


Aron Erben (Apr 19 2022 at 14:37):

Hello, I would like to concatenate two matrices together column wise, like in the following example:

import data.matrix.basic
import data.real.basic

variables
  {n p₁ p₂ : }
  (X₁ : matrix (fin n) (fin p₁) )
  (X₂ : matrix (fin n) (fin p₂) )
  (X : matrix (fin n) (fin (p₁ + p₂)) )
  (h : X = (X₁ <> X₂))

<> here represents concatenation, is there a function that does that? I'm aware matrices are functions, so I would have to concatenate their domains and ranges, right?

Callum Cassidy-Nolan (Apr 19 2022 at 16:08):

I didn't see the method for that, but if you need to implement it you can use this idea:
https://i.imgur.com/nspxD5K.jpg

Eric Wieser (Apr 19 2022 at 16:20):

This works:

def matrix.fin_append_cols
  {n p₁ p₂ : }
  (X₁ : matrix (fin n) (fin p₁) )
  (X₂ : matrix (fin n) (fin p₂) ) : matrix (fin n) (fin (p₁ + p₂)) 
| i := @fin.add_cases _ _ (λ i, ) (X₁ i) (X₂ i)

Eric Wieser (Apr 19 2022 at 16:26):

Or a more general way:

import data.matrix.basic
import data.real.basic
import logic.equiv.fin

def matrix.append_cols
  {m n₁ n₂ α : Type}
  (X₁ : matrix m n₁ α)
  (X₂ : matrix m n₂ α) : matrix m (n₁  n₂) α
| i := sum.elim (X₁ i) (X₂ i)

example
  {n p₁ p₂ : }
  (X₁ : matrix (fin n) (fin p₁) )
  (X₂ : matrix (fin n) (fin p₂) ) : matrix (fin n) (fin (p₁ + p₂))  :=
(X₁.append_cols X₂).minor id fin_sum_fin_equiv.symm

Eric Wieser (Apr 19 2022 at 16:26):

You might be interested in docs#matrix.from_blocks


Last updated: Dec 20 2023 at 11:08 UTC