改写UMFPACK算例中的压缩方式(动态)

在上篇博文基础上修改,使之A能动态

View Code
/* -------------------------------------------------------------------------- */
/* UMFPACK Copyright (c) Timothy A. Davis, CISE,                              */
/* Univ. of Florida.  All Rights Reserved.  See ../Doc/License for License.   */
/* web: http://www.cise.ufl.edu/research/sparse/umfpack                       */
/* -------------------------------------------------------------------------- */

#include <stdio.h>
#include "umfpack.h"
#include<vector>
#include<iostream>
using namespace std;

int main (void)
{
    //int A[5][5]={{2,3,0,0,0},{3,0,4,0,6},{0,-1,-3,2,0},{0,0,1,0,0},{0,4,2,0,1}};
    int    n = 5 ;
    int    Ap [ ] = {0, 2, 5, 9, 10, 12} ;
    //int *Ap=new int [n+1];
    //Ap [ ]= {0, 2, 5, 9, 10, 12} ;
    int **A=new int *[n];
    for(int k=0;k<n;k++)
        A[k]= new int [n];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            cin>>A[i][j];
    int Apm[6]={0};//相当于Ap
    double epsilon=0.00001;
    int NZnum=0;//矩阵非零元的个数
    vector<int> Axm;//相当于Ax
    vector<int> Aim;//相当于Ai
    for(int j=0;j<n;j++)
    {
        for(int i=0;i<n;i++)
        {
            if(abs(A[i][j])>epsilon)
            {
                //Axm[NZnum]=A[i][j];printf("%d\n",Axm[NZnum]);
                Axm.push_back(A[i][j]);
                Aim.push_back(i);
                NZnum++;
            }
        }
        Apm[j+1]=NZnum;
        //printf("%d\n",Apm[j+1]);
    }
    for (int i=0; i<int(Axm.size()); i++)
    {
        printf("%d ", Axm[i]);
    }
    cout<<endl;
    for (int i=0; i<int(Aim.size()); i++)
    {
        printf("%d ", Aim[i]);
    }
    cout<<endl;
    int    Ai [ ] = { 0,  1,  0,   2,  4,  1,  2,  3,   4,  2,  1,  4} ;
    double Ax [ ] = {2., 3., 3., -1., 4., 4., -3., 1., 2., 2., 6., 1.} ;
    double b [ ] = {8., 45., -3., 3., 19.} ;
    double x [5] ;
    double *null = (double *) NULL ;
    int i ;
    void *Symbolic, *Numeric ;
    (void) umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, null, null) ;
    (void) umfpack_di_numeric (Ap, Ai, Ax, Symbolic, &Numeric, null, null) ;
    umfpack_di_free_symbolic (&Symbolic) ;
    (void) umfpack_di_solve (UMFPACK_A, Ap, Ai, Ax, x, b, Numeric, null, null) ;
    umfpack_di_free_numeric (&Numeric) ;
    for (i = 0 ; i < n ; i++) printf ("x [%d] = %g\n", i, x [i]) ;
    return (0) ;
}

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2013-02-24 13:49  liang_l  阅读(256)  评论(0编辑  收藏  举报