Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #188 Matrix multiplication

Perform matrix multiplication of a real matrix a with nx rows and ny columns, a real matrix b with ny rows and nz columns and assign the value to a real matrix c with nx rows and nz columns.

use PDL::Basic qw(sequence);
use PDL::Primitive qw(matmult);
my ($nx, $ny, $nz) = (2, 3, 4);
my $A = sequence $ny, $nx;
my $B = sequence $nz, $ny;
my $C = matmult $A, $B;
(defn t [x] (apply (partial mapv vector) x))
(defn v* [u v] (reduce + (mapv * u v)))
(defn shape [a] (comp (partition-all (count a)) (map vec)))

(defn mm [a b]
    (into [] (shape a)
        (for [line a col (t b)]
            (v* line col))))

(def c (mm a b))
(require '[clojure.core.matrix :refer [mmul]])
(def c (mmul a b))
c = matmul (a,b)
import "gonum.org/v1/gonum/mat"
c := new(mat.Dense)
c.Mul(a, b)
uses typ, omv;
var
  A: array[1..m, 1..n] of ArbFloat;
  B: array[1..n, 1..p] of ArbFloat;
  C: array[1..m, 1..p] of ArbFloat;

...

omvmmm(
    A[1,1], m, n, n,
    B[1,1], p, p,
    C[1,1], p
  );
import numpy as np
c = a @ b
import numpy as np
c = np.matmul(a, b)
require 'matrix'
c = a * b
use ndarray::arr2;
let c = a.dot(&b);

New implementation...