Skip to content

Instantly share code, notes, and snippets.

@DBJDBJ
Created September 10, 2021 06:23
Show Gist options
  • Save DBJDBJ/00ae4d09fbf25555bd2fa1af53f7cef2 to your computer and use it in GitHub Desktop.
Save DBJDBJ/00ae4d09fbf25555bd2fa1af53f7cef2 to your computer and use it in GitHub Desktop.
mohapatra_matrix_multiplication transformed to ISO C
/*
made 2021 by dbj@dbj.org
https://godbolt.org/z/PjovPPoxf
*/
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define FOR(C, R) for (unsigned C = 0; C < R; ++C)
/*
https://raw.githubusercontent.com/ShrohanMohapatra/matrix_multiply_quadratic/master/matrix_multiply_test.py
Transformation to standard C (c) 2021 by dbj@dbj.org
*/
enum { N = 2 };
static void mohapatra(double A[N][N], double B[N][N], double E[N][N]) {
double maxi = 0;
FOR(i, N)
FOR(j, N) {
if (maxi < A[i][j]) maxi = A[i][j];
if (maxi < B[i][j]) maxi = B[i][j];
}
int M = (int)(log10(maxi)) + 1;
int P = (int)(log10((pow(10, (2 * M)) - 1) * N)) + 1;
double C[N] = {0}, D[N] = {0};
FOR(i, N) {
double sum_1 = 0;
FOR(j, N) sum_1 = sum_1 * (pow(10, P)) + A[i][j];
C[i] = sum_1;
}
FOR(j, N) {
double sum_1 = 0;
FOR(i, N) {
sum_1 = sum_1 * (pow(10, P)) + B[N - 1 - i][j];
}
D[j] = sum_1;
}
FOR(i, N) {
FOR(j, N) {
E[i][j] = (int)(C[i] * D[j] / (pow(10, (P * (N - 1))))) % (int)(pow(10, P));
}
}
} // mohapatra
static void printmat(double mx[N][N]) {
FOR(j, N) {
FOR(k, N) { printf(" %4.2f ", mx[j][k]); }
printf("\n");
}
}
int main(void) {
double a[2][2] = {{1, 2}, {3, 4}};
double b[2][2] = {{5, 6}, {7, 8}};
printf(
"\nThe constelation"
"\nof matrices, to check the correctness:"
"\n"
"\n | 1 2 | | 5 6 | | 19 22 |"
"\n | | x | | = | |"
"\n | 3 4 | | 7 8 | | 43 50 |"
"\n" ) ;
{
double r[2][2] = {{0, 0}, {0, 0}};
mohapatra(a, b, r);
printf("\nAfter R = mohapatra(A,B) the result is:\n\n");
printmat(r);
}
printf(
"\nAlgorithm:"
"\n"
"\nhttps://raw.githubusercontent.com/ShrohanMohapatra/matrix_multiply_quadratic/master/matrix_multiply_test.py"
"\nBSD 3-Clause License"
"\nCopyright (c) 2019, ShrohanMohapatra"
"\nAll rights reserved."
"\n"
"\nTransformation to C (c) 2021 by dbj@dbj.org"
);
return 42;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment