Matrix multiplication: Difference between revisions

Content added Content deleted
m (→‎[[ALGOL 68]]: turn parallel on)
m (→‎{{header|C}}: Syntax hilighting for C)
Line 232: Line 232:
=={{header|C}}==
=={{header|C}}==
{{works with|gcc|<nowiki>4.1.2 20070925 (Red Hat 4.1.2-27) Options: gcc -std=gnu99</nowiki>}}
{{works with|gcc|<nowiki>4.1.2 20070925 (Red Hat 4.1.2-27) Options: gcc -std=gnu99</nowiki>}}
<c>
#include <stdio.h>
#include <stdio.h>
#define dim 4 /* fixed length square matrices */
#define dim 4 /* fixed length square matrices */
const int SLICE=0; /* coder hints */
const int SLICE=0; /* coder hints */
typedef double field_t; /* field_t type is long float */
typedef double field_t; /* field_t type is long float */
typedef field_t vec_t[dim];
typedef field_t *ref_vec_t; /* address of first element */
typedef field_t vec_t[dim];
typedef field_t *ref_vec_t; /* address of first element */
typedef vec_t matrix_t[dim];
typedef vec_t matrix_t[dim];
typedef field_t *ref_matrix_t; /* address of first element */
typedef field_t *ref_matrix_t; /* address of first element */
typedef char* format;
typedef char* format;

/* define the vector/matrix_t operators */

field_t v_times_v (vec_t a, vec_t b, int step_b){
/* basically the dot product if step_b==1*/
field_t result=0;
for( int i=0; i<sizeof a; i++ )
result+= a[i]*b[i*step_b];
return result;
}

ref_vec_t v_eq_v_times_m(vec_t result, vec_t a, matrix_t b){
for( int j=0; j<sizeof b; j++ )
result[j]=v_times_v(a,&b[SLICE][j],sizeof b[SLICE] / sizeof (field_t));
return &result[SLICE];
}

ref_matrix_t m_eq_m_times_m (matrix_t result, matrix_t a, matrix_t b){
for( int k=0; k<sizeof result; k++ )
v_eq_v_times_m(&result[k][SLICE],&a[k][SLICE],b);
return &result[SLICE][SLICE];
}

/* Some sample matrices to test */
matrix_t a={{1, 1, 1, 1}, /* matrix_t A */
{2, 4, 8, 16},
{3, 9, 27, 81},
{4, 16, 64, 256}};

matrix_t b={{ 4.0 , -3.0 , 4.0/3, -1.0/4 }, /* matrix_t B */
{-13.0/3, 19.0/4, -7.0/3, 11.0/24},
{ 3.0/2, -2.0 , 7.0/6, -1.0/4 },
{ -1.0/6, 1.0/4, -1.0/6, 1.0/24}};

int main(){
matrix_t prod;
m_eq_m_times_m(prod,a,b); /* actual multiplication example of A x B */

#define field_fmt "%6.2f" /* width of 6, with no '+' sign, 2 decimals */
#define vec_fmt "{"field_fmt","field_fmt","field_fmt","field_fmt"}"
#define matrix_fmt " {"vec_fmt",\n "vec_fmt",\n "vec_fmt",\n "vec_fmt"};"
format result_fmt = " Product of a and b: \n"matrix_fmt"\n";
/* define the vector/matrix_t operators */

/* finally print the result */
field_t v_times_v (vec_t a, vec_t b, int step_b){
vprintf(result_fmt,(void*)&prod);
/* basically the dot product if step_b==1*/
}
field_t result=0;
</c>
for( int i=0; i<sizeof a; i++ )
result+= a[i]*b[i*step_b];
return result;
}
ref_vec_t v_eq_v_times_m(vec_t result, vec_t a, matrix_t b){
for( int j=0; j<sizeof b; j++ )
result[j]=v_times_v(a,&b[SLICE][j],sizeof b[SLICE] / sizeof (field_t));
return &result[SLICE];
}
ref_matrix_t m_eq_m_times_m (matrix_t result, matrix_t a, matrix_t b){
for( int k=0; k<sizeof result; k++ )
v_eq_v_times_m(&result[k][SLICE],&a[k][SLICE],b);
return &result[SLICE][SLICE];
}
/* Some sample matrices to test */
matrix_t a={{1, 1, 1, 1}, /* matrix_t A */
{2, 4, 8, 16},
{3, 9, 27, 81},
{4, 16, 64, 256}};
matrix_t b={{ 4.0 , -3.0 , 4.0/3, -1.0/4 }, /* matrix_t B */
{-13.0/3, 19.0/4, -7.0/3, 11.0/24},
{ 3.0/2, -2.0 , 7.0/6, -1.0/4 },
{ -1.0/6, 1.0/4, -1.0/6, 1.0/24}};
int main(){
matrix_t prod;
m_eq_m_times_m(prod,a,b); /* actual multiplication example of A x B */
#define field_fmt "%6.2f" /* width of 6, with no '+' sign, 2 decimals */
#define vec_fmt "{"field_fmt","field_fmt","field_fmt","field_fmt"}"
#define matrix_fmt " {"vec_fmt",\n "vec_fmt",\n "vec_fmt",\n "vec_fmt"};"
format result_fmt = " Product of a and b: \n"matrix_fmt"\n";
/* finally print the result */
vprintf(result_fmt,(void*)&prod);
}
Output:
Output:
Product of a and b:
Product of a and b: