arrayscmathmatrix

From 2D Arrays to 2x2 Matrix in C


I am trying to make a 2x2 matrix from two 2D arrays that contain values in C.The Matrix to be produced, named overlap and is a 2x2 matrix, has elements that are functions of each D and A Arrays. I have written the following code and its output shown below. I don't know what I am doing wrong and was wondering if whoever reads this can help a misguided new C learner. P.S. I know I can write each element explicitly then put them into a matrix without using an array; however, I plan to use this skeleton as a foundation for an automation caculcation code for bigger input.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define BASISFUN 2
#define PI 3.14159265358979323846
void overlaps(double overlap[][2], double d[][2], double a[][2]) {
  printf("\nThe Overlap Matrix is\n");
  for (int i = 0; i < BASISFUN; i++) {
    for (int j = 0; j < BASISFUN; j++) {
      overlap[i][j] = 0.0;
      if (i == j && i == 0) {
        overlap[0][0] =
            pow(d[0][0], 2.0) * pow((PI / (a[0][0] + a[0][0])), 1.5);
      } else if (i == j && i == 1) {
        overlap[1][1] =
            pow(d[1][1], 2.0) * pow((PI / (a[1][1] + a[1][1])), 1.5);
      } else if (i != j && i == 1) {
        overlap[1][0] =
            pow(d[1][0], 2.0) * pow((PI / (a[1][0] + a[1][0])), 1.5);
      } else {
        overlap[0][1] =
            pow(d[0][1], 2.0) * pow((PI / (a[0][1] + a[0][1])), 1.5);
      }
      printf("%.4f\t", overlap[i][j]);
    }
    printf("\n");
  }
}
int main() {
  double A1 = 0.532149;
  double A2 = 4.097728;
  double D1 = 0.82559;
  double D2 = 0.28317;
  double a[1][2] = {A1, A2};
  double d[1][2] = {D1, D2};
  double overlap[2][2];
  overlaps(overlap, d, a);
  return 0;
}

OUTPUT: The Orbital Coefficient Matrix is

3.4567  0.0190  
3.6998  44.0676 

However, the product is supposed to be as follows when using Matlab, which is 100% correct:

3.4567    0.1307
0.1307    0.0190

Solution

  • The a and d dimensions and calculations of overlap are incorrect:

    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BASISFUN 2
    #define PI 3.14159265358979323846
    
    void overlaps(double overlap[][BASISFUN], double d[][BASISFUN], double a[][BASISFUN])
    {
        overlap[0][0] = pow(d[0][0], 2.0) * pow((PI / (a[0][0] + a[0][0])), 1.5);
        overlap[0][1] = d[0][0] * d[0][1] * pow((PI / (a[0][0] + a[0][1])), 1.5);
        overlap[1][0] = d[1][0] * d[1][1] * pow((PI / (a[1][0] + a[1][1])), 1.5);
        overlap[1][1] = pow(d[1][1], 2.0) * pow((PI / (a[1][1] + a[1][1])), 1.5);
    
        printf("The Overlap Matrix is:\n");
        for (int i = 0; i < BASISFUN; i++)
        {
            for (int j = 0; j < BASISFUN; j++)
            {
                printf("%.8f\t", overlap[i][j]);
            }
            printf("\n");
        }
    }
    
    int main()
    {
        double A1 = 0.532149, A2 = 4.097728, D1 = 0.82559, D2 = 0.28317;
    
        double a[BASISFUN][BASISFUN] = {{A1, A2}, {A1, A2}};
        double d[BASISFUN][BASISFUN] = {{D1, D2}, {D1, D2}};
    
        double overlap[BASISFUN][BASISFUN];
        overlaps(overlap, d, a);
    
        return 0;
    }
    
    
    

    Prints

    The Overlap Matrix is:
    3.45667690  0.13067193  
    0.13067193  0.01903091  
    

    Comments


    Edit:

    You can further expand it to higher dimensions, by simply assigning proper values to A[BASISFUN] and D[BASISFUN] and modifying the loops accordingly:

    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BASISFUN 6
    #define PI 3.14159265358979323846
    
    void overlaps(
        double overlap[][BASISFUN],
        double d[][BASISFUN],
        double a[][BASISFUN])
    {
    
        for (int i = 0; i < BASISFUN; i++)
        {
            overlap[i][i] = pow(d[i][i], 2.0) * pow((PI / (a[i][i] + a[i][i])), 1.5);
        }
    
        for (int i = 0; i < BASISFUN; i++)
        {
            for (int j = 0; j < BASISFUN; j++)
            {
                if (i != j)
                {
                    overlap[i][j] =
                        d[i][j] * d[j][i] * pow((PI / (a[i][j] + a[j][i])), 1.5);
                }
            }
        }
    
        printf("The Overlap Matrix is:\n");
        for (int i = 0; i < BASISFUN; i++)
        {
            for (int j = 0; j < BASISFUN; j++)
            {
                printf("%.8f\t", overlap[i][j]);
            }
            printf("\n");
        }
    }
    
    int main()
    {
        double a[BASISFUN][BASISFUN];
        double d[BASISFUN][BASISFUN];
    
        double A[BASISFUN] = {0.532149, 4.097728, 0.00001, 0.00001, 0.00001, 0.00001};
        double D[BASISFUN] = {0.82559, 0.28317, 0.00001, 0.00001, 0.00001, 0.00001};
    
        for (int i = 0; i < BASISFUN; i++)
        {
            for (int j = 0; j < BASISFUN; j++)
            {
                a[i][j] = A[j];
                d[i][j] = D[j];
            }
        }
    
        double overlap[BASISFUN][BASISFUN];
        overlaps(overlap, d, a);
    
        return 0;
    }
    
    

    Prints

    The Overlap Matrix is:
    3.45667690  0.13067193  0.00011842  0.00011842  0.00011842  0.00011842  
    0.13067193  0.01903091  0.00000190  0.00000190  0.00000190  0.00000190  
    0.00011842  0.00000190  0.00622558  0.00622558  0.00622558  0.00622558  
    0.00011842  0.00000190  0.00622558  0.00622558  0.00622558  0.00622558  
    0.00011842  0.00000190  0.00622558  0.00622558  0.00622558  0.00622558  
    0.00011842  0.00000190  0.00622558  0.00622558  0.00622558  0.00622558