示例#1
0
文件: srchmat.c 项目: MiCHiLU/algo
int main()
{
    int i, j, x;
    static matrix a = {{1, 2, 3, 4}, {2, 4, 6, 8}, {3, 6, 9, 12}};

    printf("    1  2  3  4\n"
           "A = 2  4  6  8  (i = 0, 1, 2; j = 0, 1, 2, 3)\n"
           "    3  6  9 12\n"
           "何を探しますか? ");
    scanf("%d", &x);
    if (searchmat(x, a, 0, 2, 0, 3, &i, &j) == FOUND)
        printf("(i, j) = (%d, %d)\n", i, j);
    else
        printf("見つかりません\n");
    return EXIT_SUCCESS;
}
示例#2
0
void CMatrix::matalg(double *C, int r, int s,int m, int n, int p, int opt, double eps)
{
	double *R,*Z;
	int i,j,Det, ii,jj, k, psrank;
	bool Done;
	if (opt==1) //inverse calculation;
	{
		ident(C,r,s,1,n+1,n,1);
	}
	R=new double[(r)*(s)];
	Z=new double[3];
	for(i=1;i<=r;i++) //random error initialization
	{
		for(int j=1;j<=s;j++)
		{
			R[s*i+j]=0.000000000001*(rand()-500000000000)*eps*C[s*i+j];
		}
	}
	for(j=1;j<=n;j++) // Set row permutation matrix to identity
	{
		C[s*r+j]=j;
	}
	//forward solution
	Det=1; Done=false; i=0;
	while((i<m)&&(!Done))
	{
		Z=searchmat(C,r,s,i+1,i+1,m,n);jj=Z[1];ii=Z[2];
		if(sqrt(Z[0]*Z[0])>sqrt(R[s*ii+jj]*R[s*ii+jj]))
		{
			i++;
		//switch row
		Det=Det*switchrow(C,r,s,i,ii,i,s);
		switchrow(R,r,s,i,ii,i,s);
		// switch columns
		Det=Det*switchcol(C,r,s,i,jj,1,r);
		switchcol(R,r,s,i,jj,1,r);
		// divide by pivot element
		scaler(C,R,r,s,i,i);
		Det=Det*scale(C,r,s,i,i);
			for (int k=i+1 ;k<=m; k++)
			{
			// reduce row k against row i
				pivotr(C,R,r,s,i,k,i+1);
				pivot(C,r,s,i,k,i+1);
			}
		}
		else
		{
			Done=true ;
		}
	}
	psrank=i;
		// end forward solution; begin consistency check
		if (psrank<m)
		{
			Det=0;
			for (j=1;j<=p;j++)
			{

				// check that right hand sides are 0 for i>psrank
				Z=searchmat(C,r,s,psrank+1,n+j,m,n+j);
				int a=Z[1];
				int b=Z[2];

				if(sqrt(Z[0]*Z[0])>sqrt(R[a*s+b]*R[a*s+b]))
				{		;//printf("Right hand side %d is inconsistent",j);

				}
			}
		}
	// equations are consistent, do back solution
		for (j=psrank;j>=2;j--)
		{
			for(i=1;i<=j-1;i++)
			{				
				pivotr(C,R,r,s,j,i,psrank+1);
				pivot(C,r,s,j,i,psrank+1);
				C[(s)*i+j]=0; R[(s)*i+j]=0;
			}
		}
	// end back solution, insert minus identity in basis
	if (psrank<n)
	{
	// fill bottom of basis matrix with -I
		ident(C,r,s,psrank+1,psrank+1,n-psrank,-1);
	// fill under right-hand sides with zeroes
		for(i=psrank+1 ;i<=n;i++)
		{
			for (j=n+1 ;j<=s;j++)
			{
				C[s*i+j]=0; 
			}
		}
	// fill under R matrix with zeroes
		for(i=psrank+1;i<=n;i++)
		{
			for(j=n-psrank;j<=s;j++)
			{
				R[s*i+j]=0;
			}
		}
	}
	// permute rows prior to output
	*Z=scramble(C,r,s,n);
	// copy row r of C to row r of R
	for(j=1;j<=n;j++)
	{
		R[(s)*r+j]=C[(s)*r+j];
	}
	scramble(R,r,s,n);
//	return(Det,psrank,evalm(R));
	
	}