Matrix Vandermonde Code

I am very interesting with polyfit code in Matlab. I try to convert this polyfit code in Matlab to C. When I try create code polyfit from Matlab to C, I get a function vander in Matlab. vander is Matlab function to create matrix vandermonde. After searching about matrix vandermonde, I try to create matrix vandermonde code in C. In linear algebra, a Vandermonde matrix, named after Alexandre-Théophile Vandermonde, is a matrix with the terms of a geometric progression in each row, i.e., an m × n matrix (from wikipedia). I get this equation to create matrix vandermonde code :

Output from this matrix vandermonde is a matrix 2D. You can read my last post about how to create 2D array in C/C++ in here. From my example to create Matrix Vandermonde, I create input array 1D with value [1 2 3 4 5]. This is my matrix vandermonde code in C. Save this code with name vandermonde.c.

/* matrix vandermonde code.c
* create by toto_plg@yahoo.com
* 07/25/2011
* http://toto-share.com
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int **array2_create(int row, int col); /* create array 2D */
void array2_free(int **arr);           /* free array 2D */
void printArray2(char *var, int **arr, int nx, int ny);
void printArray1(char *var, int *arr, int nx);
void createVandermonde(int *iarr, int **vander, int nx);

int main(int argc, char **argv)
{
  int nx;
  int *iarr, **vandermonde;
  int i;
  
  nx = 5;
  
  vandermonde = array2_create(nx, nx); /* create output matrix vandermonde */
  iarr = (int*) calloc (nx, sizeof(int));
  for(i=0; i<nx; i++) //fill iarr by value
    iarr[i] = i+1;
  
  createVandermonde(iarr, vandermonde, nx); /* vandermonde process */
  
  printArray1("iarr", iarr, nx); //print input array
  printArray2("vandermonde", vandermonde, nx, nx); //print input array
  
  /* free allocated memory */
  free(iarr);
  array2_free(vandermonde);
}

void createVandermonde(int *iarr, int **vander, int nx)
{
  int i, j;
  for(i=0; i<nx; i++)
    for(j=0; j<nx; j++)
      vander[i][j] = pow(iarr[i], nx-j-1);
}

int **array2_create(int row, int col)
{
    int i;
    int **arr=NULL;
 
    /* allocate pointers to rows */
    arr = (int **) malloc((row*sizeof(int*)));
    if (!arr) {
        fprintf(stderr, "matrixi_create : error allocation row");
        exit(0);
    }
 
    /* allocate rows and set pointers to them */
    arr[0]=(int*) malloc((row*col)*sizeof(int));
    if (!arr[0]) {
        fprintf(stderr, "matrixi_create : error allocation column");
        exit(0);
    }
 
    for(i=1; i<row; i++)
        arr[i]=arr[i-1] + col;
 
    /* return pointer to array of pointers to rows */
    return arr;
}

void array2_free(int **arr)
{
    free (arr[0]);
    free (arr);
}

void printArray2(char *var, int **arr, int nx, int ny)
{
  int i, j;
  printf("Array %s \n", var);
  for(i=0; i<ny; i++)
  {
    for(j=0; j<nx; j++)
      printf("%i \t", arr[i][j]);
    
    printf("\n");
  }
  printf("\n\n");
}

void printArray1(char *var, int *arr, int nx)
{
  int i;
  printf("Array %s \n", var);
  for(i=0; i<nx; i++)
    printf("%i \t", arr[i]);
    
    printf("\n\n");
}

You can save this code and compile this matrix vandermonde code with command :

gcc vandermonde.c -lm -o vandermonde

This is output from my matrix vandermonde code in C :

toto@toto-laptop:~/Documents$ ./vandermonde 
Array iarr 
1       2       3       4       5 

Array vandermonde 
1       1       1       1       1 
16      8       4       2       1 
81      27      9       3       1 
256     64      16      4       1 
625     125     25      5       1

Output from this my  matrix vandermonde code in C is equal with output from Matlab.

Add a Comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.