this is the formula but it has the in2d again
Code:
/ Multiplication.
// Need (wm1 == hm2) && (hm1 == hmr) && (wm2 == wmr).
void matrix_mult(double m1[], double m2[], double mr[],
int hm1, int wm1, int wm2)
{
int i, j, k;
double sum;
for(i = 0; i < hm1; ++i)
for(j = 0; j < wm2; ++j) {
sum = 0;
for(k = 0; k < wm1; ++k)
sum += m1[in2d(i, k, wm1)] * m2[in2d(k, j, wm2)];
mr[in2d(i, j, wm2)] = sum;
}
return;
}
i tried to change the code like you did last time and i did this but it doesnt work
Code:
/*matrix multiplication*/
for(i = 0; i < 9; i++){
for(j = 0; j < 9 ; j++) {
for(k = 0; k < 2*n; k++){
W[i][j]+= (A[i][k])*(At[k][j]);
}
}
}
ok and heres my whole program...if you take out the multiplication it works so far
Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "linear_algebra.h"
int main(){
int n,i,j,k;
double **a; /* this is the array of points name */
double **A; /* this is the array of 2n x 9*/
double **At; /* this is the array of A transpose */
double **W; /*this is the 9 x 9 arrray*/
int num_rows; /* this variable will be used for the first dimension */
int num_col; /* this variable will be used for the second dimension */
FILE * ifile;
ifile = fopen ("points.txt","r");
fscanf(ifile, "%d\n", &n);/*read in the number of points N.*/
printf("%d\n", n);
num_rows = n;
num_col = 6;
/* allocate storage for an array of pointers */
a = malloc(num_rows * sizeof(double *));
/* for each pointer, allocate storage for an array of doubles */
for (i = 0; i < num_rows; i++) {
a[i] = malloc(num_col * sizeof(double));
}
/* assign the value to each element */
for (i = 0; i < num_rows; i++) {
for (j = 0; j < num_col; j++) {
fscanf(ifile,"%lf ", &a[i][j]);
}
}
/* malloc again for 2nd array */
A = malloc((2*n) * sizeof(double *));
for (i = 0; i < (2*n); i++) {
A[i] = malloc(9 * sizeof(double));
}
/* create a 2*n X 9 matrix*/
for (i = 0; i < num_rows; i++) {
A[2*i][0]=0;
A[2*i][1]=0;
A[2*i][2]=0;
A[2*i][3]=(-a[i][5])*a[i][0];
A[2*i][4]=(-a[i][5])*a[i][1];
A[2*i][5]=(-a[i][5])*a[i][2];
A[2*i][6]=(a[i][4])*a[i][0];
A[2*i][7]=(a[i][4])*a[i][1];
A[2*i][8]=(a[i][4])*a[i][2];
A[2*i+1][0]=(a[i][5])*a[i][0];
A[2*i+1][1]=(a[i][5])*a[i][1];
A[2*i+1][2]=(a[i][5])*a[i][2];
A[2*i+1][3]=0;
A[2*i+1][4]=0;
A[2*i+1][5]=0;
A[2*i+1][6]=(-a[i][3])*a[i][0];
A[2*i+1][7]=(-a[i][3])*a[i][1];
A[2*i+1][8]=(-a[i][3])*a[i][2];
}
/* malloc again for 3rd array */
At = malloc((2*n) * sizeof(double *));
for (i = 0; i < (2*n); i++) {
At[i] = malloc(9 * sizeof(double));
}
/* matrix transpose*/
for (i = 0; i < 2*n; i++) {
for (j = 0; j < 9; j++) {
At[i][j]=A[j][i];
}
}
/* malloc again for 4th array */
W = malloc((9) * sizeof(double *));
for (i = 0; i < (9); i++) {
W[i] = malloc(9 * sizeof(double));
}
/*matrix multiplication*/
for(i = 0; i < 9; i++){
for(j = 0; j < 9 ; j++) {
for(k = 0; k < 2*n; k++){
W[i][j]+= (A[i][k])*(At[k][j]);
}
}
}
for (i = 0; i < 9; i++) {
for (j = 0; j <9 ; j++) {
printf("%6.4lf ", W[i][j]);
}
}
/* now for each pointer, free its array of dbles */
for (i = 0; i < num_rows; i++) {
free(a[i]);
}
/* now free the array of pointers */
free(a);
/* free for 2nd array */
for (i = 0; i < (n*2); i++) {
free(A[i]);
}
free(A);
/* free for 3rd array */
for (i = 0; i < (9); i++) {
free(At[i]);
}
free(At);
/* free for 4th array */
for (i = 0; i < (9); i++) {
free(W[i]);
}
free(W);
fclose(ifile);
return 0;
}