#include <stdio.h>
#include <glm/vec3.hpp>
#include <glm/mat3x3.hpp>
int main(int argc, char *argv[]) {
using namespace glm;
mat3x3 A = mat3x3(vec3(1.f, 4.f, -3.f), vec3(2.f, -5.f, 2.f), vec3(1.f, 6.f, 4.f));
printf("%f %f %f\n", A[0][0], A[1][0], A[2][0]);
printf("%f %f %f\n", A[0][1], A[1][1], A[2][1]);
printf("%f %f %f\n", A[0][2], A[1][2], A[2][2]);
printf("\n");
mat3x3 B = mat3x3(vec3(1.f, 0.f, 1.f), vec3(2.f, 1.f, 1.f), vec3(-1.f, 0.f, 0.f));
printf("%f %f %f\n", B[0][0], B[1][0], B[2][0]);
printf("%f %f %f\n", B[0][1], B[1][1], B[2][1]);
printf("%f %f %f\n", B[0][2], B[1][2], B[2][2]);
printf("\n");
mat3x3 ans = A*B;
printf("%f %f %f\n", ans[0][0], ans[1][0], ans[2][0]);
printf("%f %f %f\n", ans[0][1], ans[1][1], ans[2][1]);
printf("%f %f %f\n", ans[0][2], ans[1][2], ans[2][2]);
return 0;
}
|
#include <stdio.h>
#include <string.h>
#include <cblas.h>
int main(int argc, char *argv[]) {
double alpha = 1.;
#define M 3
#define N 3
#define K 3
double A[M][K] = {{1., 4., -3.}, {2., -5., 2.}, {1., 6., 4.}};
double B[K][N] = {{1., 0., 1.}, {2., 1., 1.}, {-1., 0., 0.}};
double beta = 0.;
double C[M][N];
memset(C, 0x00, sizeof(C));
#if 1 // show mat
printf("%f %f %f\n", A[0][0], A[1][0], A[2][0]);
printf("%f %f %f\n", A[0][1], A[1][1], A[2][1]);
printf("%f %f %f\n", A[0][2], A[1][2], A[2][2]);
printf("\n");
printf("%f %f %f\n", B[0][0], B[1][0], B[2][0]);
printf("%f %f %f\n", B[0][1], B[1][1], B[2][1]);
printf("%f %f %f\n", B[0][2], B[1][2], B[2][2]);
printf("\n");
#endif
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, M, N, K,
alpha, (double *)A, M, (double *)B, K, beta, (double *)C, M);
#undef M
#undef K
#if 1 // show
{
#if 1
const double (*ans)[N] = C;
printf("%f %f %f\n", ans[0][0], ans[1][0], ans[2][0]);
printf("%f %f %f\n", ans[0][1], ans[1][1], ans[2][1]);
printf("%f %f %f\n", ans[0][2], ans[1][2], ans[2][2]);
#else
const double *ans = (double *)C;
printf("%f %f %f\n", ans[0], ans[3], ans[6]);
printf("%f %f %f\n", ans[1], ans[4], ans[7]);
printf("%f %f %f\n", ans[2], ans[5], ans[8]);
#endif
}
#endif
#undef N
return 0;
}
|
PROGRAM mat_prog2
IMPLICIT NONE
REAL(8), DIMENSION(3,3) :: A = RESHAPE((/1.D0, 4.D0, -3.D0, 2.D0, -5.D0, 2.D0, 1.D0, 6.D0, 4.D0/), (/3, 3/))
REAL(8), DIMENSION(3,3) :: B = RESHAPE((/1.D0, 0.D0, 1.D0, 2.D0, 1.D0, 1.D0, -1.D0, 0.D0, 0.D0/), (/3, 3/))
REAL(8), DIMENSION(3,3) :: ans
INTEGER :: i, j
DO i = 1, 3
WRITE(*,*) (A(i,j), j = 1, 3)
END DO
WRITE(*,*) ''
DO i = 1, 3
WRITE(*,*) (B(i,j), j = 1, 3)
END DO
WRITE(*,*) ''
ans = MATMUL(A, B)
DO i = 1, 3
WRITE(*,*) (ans(i,j), j = 1, 3)
END DO
END PROGRAM mat_prog2
|
(%i1) A: matrix([1, 2, 1], [4, -5, 6], [-3, 2, 4]);
[ 1 2 1 ]
[ ]
(%o1) [ 4 - 5 6 ]
[ ]
[ - 3 2 4 ]
(%i2) B: matrix([1, 2, -1], [0, 1, 0], [1, 1, 0]);
[ 1 2 - 1 ]
[ ]
(%o2) [ 0 1 0 ]
[ ]
[ 1 1 0 ]
(%i3) A.B;
[ 2 5 - 1 ]
[ ]
(%o3) [ 10 9 - 4 ]
[ ]
[ 1 0 3 ]
|
octave:1> A = [1 2 1; 4 -5 6; -3 2 4]
A =
1 2 1
4 -5 6
-3 2 4
octave:2> B = [1 2 -1; 0 1 0; 1 1 0]
B =
1 2 -1
0 1 0
1 1 0
octave:3> A*B
ans =
2 5 -1
10 9 -4
1 0 3
|
In [1]: import numpy as np
In [2]: A = np.array([[1, 2, 1], [4, -5, 6], [-3, 2, 4]])
In [3]: B = np.array([[1, 2, -1], [0, 1, 0], [1, 1, 0]])
In [4]: A.dot(B)
Out[4]:
array([[ 2, 5, -1],
[10, 9, -4],
[ 1, 0, 3]])
In [5]: A@B
Out[5]:
array([[ 2, 5, -1],
[10, 9, -4],
[ 1, 0, 3]])
|
> (A <- matrix(c(1, 4, -3, 2, -5, 2, 1, 6, 4), ncol=3))
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 4 -5 6
[3,] -3 2 4
> (B <- matrix(c(1, 0, 1, 2, 1, 1, -1, 0, 0), ncol=3))
[,1] [,2] [,3]
[1,] 1 2 -1
[2,] 0 1 0
[3,] 1 1 0
> A%*%B
[,1] [,2] [,3]
[1,] 2 5 -1
[2,] 10 9 -4
[3,] 1 0 3
|