sparse equation system

sparse equation system

Hi!

In the below code, I have declared one matrix and one vector: A[3][3] and B[3]. The  matrix A is symmetric from main diagonal. I converted the matrix A from dense format to CSR format, and from CSR format to coordinate format using the function mkl_dcsrcoo. All conversions are ok, but I can not solve correctly the system AxY=B. I obtained a wrong solution. The correct solution is: Y=[0.5 0.5 0.5]

How can I obtain the correct solution?

The code is following:

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
#include<math.h>
#include "mkl.h"
#include "mkl_lapacke.h"

int _tmain(int argc, _TCHAR* argv[])
{
	double A[3][3] = {{2.5,-0.5,0.},{-0.5,5,-0.5},{0,-0.5,2.5}};
	double B[3]={1.0,2.0,1.0};
	MKL_INT  rows = 3, cols = 3, nnz = 7,info;
	MKL_INT job[6]={0,0,0,2,nnz,2}; 
	//for conversion of matrix A to CSR format
	double  *arrV      = new double[nnz]; 
    MKL_INT *Columns   = new MKL_INT[nnz];
    MKL_INT *Rows      = new MKL_INT[rows+1];

	mkl_ddnscsr(job,&rows, &cols, A[0], &rows, arrV, Columns, Rows,&info);

	printf("\Values\n");
	for(int i=0;i<nnz;i++)
		printf("%.3f ",arrV[i]);
	printf("\nColumns\n");
	for(int i=0;i<nnz;i++)
		printf("%d ",Columns[i]);
 	printf("\nRows\n");
	for(int i=0;i<rows+1;i++)
		printf("%d ",Rows[i]);
	printf("\n");
	
	//convert to column format
	double  *acoo      = new double[nnz]; 
	int     *rowind    = new int[nnz]; 
	int     *colind	   = new int[nnz]; 
	
	MKL_INT job1[6]={0,0,0,0,nnz,3}; 
	mkl_dcsrcoo(job1,&rows,arrV,Columns,Rows,&nnz,acoo,rowind,colind,&info);
	printf("info mkl_dcsrcoo=%d\nacoo\n",info);
	for(int i=0;i<nnz;i++)
		printf("%.3f ",acoo[i]);
	printf("\n");
	for(int i=0;i<nnz;i++)
		printf("%d ",rowind[i]);
	printf("\n");
	for(int i=0;i<nnz;i++)
		printf("%d ",colind[i]);

	MKL_INT colsC=3;
	double alpha=1;
	double *Y=new double[3];
	mkl_dcoosm ("n",&rows,&colsC,&alpha,"TUNC",acoo,rowind,colind,&nnz,B,&rows,Y,&colsC);
	printf("\n");
	for(int i=0;i<3;i++)
		printf("%.3f ",Y[i]);

	delete[] arrV;
	delete[] Columns;
	delete[] Rows;
	delete[] acoo;
	delete[] rowind;
	delete[] colind;
	_getch();
	return 0;
}

 

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

For the MATDESCRA argument, you gave "TUNC". The first letter signifies that the matrix being passed is upper triangular. This would make sense only if your matrix, which is a square matrix, has already been factorized into an LU or LDU product. Please note that with some Lapack/MKL routines a symmetric matrix is passed with the convention of including only the upper triangular part. However, this part is NOT the U factor that I just mentioned.

Leave a Comment

Please sign in to add a comment. Not a member? Join today