Esempio n. 1
0
int main(int, char**)
{
    typedef mtl::dense2D<double>       Matrix;
    typedef mtl::dense_vector<double>  Vector;
    
    Matrix                             A(4, 4), L(4, 4), U(4, 4), AA(4, 4);
    Vector	       		       v(4);
    double 			       c=1.0;   
  
    for (unsigned i= 0; i < 4; i++)
	for(unsigned j= 0; j < 4; j++) {
	    U[i][j]= i <= j ? c * (i+j+2) : (0);
	    L[i][j]= i > j ? c * (i+j+1) : (i == j ? (1) : (0));
	}
    
    std::cout << "L is:\n" << L << "U is:\n" << U;
    A= L * U;
    std::cout << "A is:\n" << A;
    AA= adjoint(A);
   
    for (unsigned i= 0; i < 4; i++)
	v[i]= double(i);

    Vector b( A*v ), b2( adjoint(A)*v );

    Matrix LU(A);
    lu(LU);
    std::cout << "LU decomposition of A is:\n" << LU;

    Matrix B( lu_f(A) );
    std::cout << "LU decomposition of A (as function result) is:\n" << B;
    
    Vector v1( lu_solve_straight(A, b) );
    std::cout << "v1 is " << v1 << "\n";

    Vector v2( lu_solve(A, b) );
    std::cout << "v2 is " << v2 << "\n";
    
    mtl::dense_vector<unsigned> P;
    lu(A, P);
    std::cout << "LU with pivoting is \n" << with_format(A, 5, 2) << "Permutation is " << P << "\n";
    Vector v3( lu_apply(A, P, b) );
    std::cout << "v3 is " << v3 << "\n";
    
    Vector v4(lu_adjoint_apply(A, P, b2));
    std::cout << "v4 is " << v4 << "\n";
   
    Vector v5(lu_adjoint_solve(AA, b));
    std::cout << "v5 is " << v5 << "\n";
       
    return 0;
}
Esempio n. 2
0
/* 
 *   inverse( original_matrix, inverse_matrix )
 * 
 *    calculate the inverse of a 4x4 matrix
 *
 *     -1     
 *     A  = ___1__ adjoint A
 *         det A
 */
bool inverse( Matrix4 *in, Matrix4 *out )
{
    int i, j;
    double det;

    /* calculate the adjoint matrix */

    adjoint( in, out );

    /*  calculate the 4x4 determinant
     *  if the determinant is zero, 
     *  then the inverse matrix is not unique.
     */

    det = det4x4( in );

    if (fabs( det ) < SMALL_NUMBER)
		return false;

    /* scale the adjoint matrix to get the inverse */

    for (i=0; i<4; i++)
		for(j=0; j<4; j++)
	    out->element[i][j] = out->element[i][j] / det;
	return true;
}
        Matrix4 Matrix4::inverse() const
        {
            if ( mState == ISIDENTITY )
                return * this;

            return adjoint() * ( 1.0f / determinant() );
        }
Esempio n. 4
0
sequence<T> adjoint( sequence<T> X )
{
	int t1 = X.t1();
	int t2 = X.t2();
	for( int t = t1; t <= t2; t++ )
		X(t) = adjoint(X(t));
	return X.timereverse();
}
Esempio n. 5
0
void test_adjoint()
{
  for(int i = 0; i < g_repeat; i++) {
    CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
    CALL_SUBTEST_2( adjoint(Matrix3d()) );
    CALL_SUBTEST_3( adjoint(Matrix4f()) );
    CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,50), internal::random<int>(1,50))) );
    CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,50), internal::random<int>(1,50))) );
    CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,50), internal::random<int>(1,50))) );
  }
  // test a large matrix only once
  CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );

#ifdef EIGEN_TEST_PART_4
  {
    MatrixXcf a(10,10), b(10,10);
    VERIFY_RAISES_ASSERT(a = a.transpose());
    VERIFY_RAISES_ASSERT(a = a.transpose() + b);
    VERIFY_RAISES_ASSERT(a = b + a.transpose());
    VERIFY_RAISES_ASSERT(a = a.conjugate().transpose());
    VERIFY_RAISES_ASSERT(a = a.adjoint());
    VERIFY_RAISES_ASSERT(a = a.adjoint() + b);
    VERIFY_RAISES_ASSERT(a = b + a.adjoint());

    // no assertion should be triggered for these cases:
    a.transpose() = a.transpose();
    a.transpose() += a.transpose();
    a.transpose() += a.transpose() + b;
    a.transpose() = a.adjoint();
    a.transpose() += a.adjoint();
    a.transpose() += a.adjoint() + b;
  }
#endif
}
	M invert(const ArithSqMat4x4Float<V,M>& in)
	{
		double det = determinant( in );
		if (is_tiny(det)) 
			throw(Mat4x4fSingular("Tried to invert Singular matrix"));
		M out = adjoint(in);
		out/=det;
		return out;
	}
Esempio n. 7
0
bool glmatrixf::invert(const glmatrixf &m, float mindet)
{
    float det = m.determinant();
    if(fabs(det) < mindet) return false;
    adjoint(m);
    float invdet = 1/det;
    loopi(16) v[i] *= invdet;
    return true;
}
Esempio n. 8
0
bool glmatrixf::invert(const glmatrixf &m, float mindet)
{
    float a1 = m.v[0], b1 = m.v[4], c1 = m.v[8], d1 = m.v[12]; 
    adjoint(m);
    float det = a1*v[0] + b1*v[1] + c1*v[2] + d1*v[3]; // float det = m.determinant(); 
    if(fabs(det) < mindet) return false;
    float invdet = 1/det;
    loopi(16) v[i] *= invdet;
    return true;
}
Esempio n. 9
0
bool Matrix::inverse(Matrix& out) const
{
	float det = determinant();
	if(fabsf(det) < EPSILON)
		return false;
	
	out = adjoint();
	out *= 1.0f / det;
	return true;
}
Esempio n. 10
0
Matrix4 Matrix4::inverse() const {
    

    Matrix4 A = adjoint();

    
    
	float det = A.column(0).dot(row(0));

	return A * (1.0f / det);
}
Esempio n. 11
0
Matrix4 Matrix4::inverse() const {
    // Inverse = adjoint / determinant

    Matrix4 A = adjoint();

    // Determinant is the dot product of the first row and the first row
    // of cofactors (i.e. the first col of the adjoint matrix)
    float det = A.column(0).dot(row(0));

    return A * (1.0f / det);
}
Esempio n. 12
0
File: mat3.cpp Progetto: 2asoft/xray
double invert(Mat3& inv, const Mat3& m)
{
    Mat3 A = adjoint(m);
    double d = A[0] * m[0];

    if( d==0.0 )
	return 0.0;

    inv = transpose(A) / d;
    return d;
}
Esempio n. 13
0
    void adjoint_solve(const VectorIn& x, VectorOut& y) const
    {
	mtl::vampir_trace<5059> tracer;
	y.checked_change_resource(x);
	pc1.adjoint_solve(x, y);

	VectorOut &r= create_r(y), &d= create_d(y);
	r= x;
	r-= adjoint(A) * y;

	pc2.adjoint_solve(r, d);
	y+= d;
    }
Esempio n. 14
0
void RG_trace(int *sign,int reta[4],int cmp[4])
{

  RG_gamma S, T, eta, etap;
  gamma_phase gS, gadjS, gT, geta, getap, gtmp1, gtmp2;
  int rS[4], rT[4],radjS[4],retap[4];

  S = cmp[0];
  T = cmp[1];

  etap =  int_to_gamma(cmp[2]);

  gamma_to_int(S, rS);
  gamma_to_int(T, rT);

  gS.g = S;
  gS.sign = 1;
  gT.g = T;
  gT.sign = 1;


  gadjS = adjoint(&gS);
  gamma_to_int(gadjS.g, radjS);
    getap.g = etap;
    getap.sign = 1;
    gtmp1 = gamma_x_gamma(&gadjS, &getap);
    gtmp2 = gamma_x_gamma(&gtmp1, &gT);

    gamma_to_int(etap, retap);

/*
fprintf(stderr,"(%d,%d,%d,%d) == (%d,%d,%d,%d) \n",
	   rvs[cmp[2]][0],rvs[cmp[2]][1],rvs[cmp[2]][2],rvs[cmp[2]][3],
	   retap[0],retap[1],retap[2],retap[3]); 
 */ 

  gamma_to_int(gtmp2.g, reta);

/*
fprintf(stderr,"Tr[ S^dag(%d,%d,%d,%d) etap(%d,%d,%d,%d) T(%d %d %d %d) eta^dag(%d %d %d %d) ] = %2d\n",
	   rS[0],rS[1],rS[2],rS[3],
	   retap[0],retap[1],retap[2],retap[3], 
	   rT[0],rT[1],rT[2],rT[3],
	   reta[0],reta[1],reta[2],reta[3],
	   gtmp2.sign
	   );
 */
  *sign = gtmp2.sign;

return; 
}
Esempio n. 15
0
void test_adjoint()
{
    for(int i = 0; i < g_repeat; i++) {
        CALL_SUBTEST( adjoint(Matrix<float, 1, 1>()) );
        CALL_SUBTEST( adjoint(Matrix3d()) );
        CALL_SUBTEST( adjoint(Matrix4f()) );
        CALL_SUBTEST( adjoint(MatrixXcf(4, 4)) );
        CALL_SUBTEST( adjoint(MatrixXi(8, 12)) );
        CALL_SUBTEST( adjoint(MatrixXf(21, 21)) );
    }
    // test a large matrix only once
    CALL_SUBTEST( adjoint(Matrix<float, 100, 100>()) );
}
Esempio n. 16
0
main ()
{
    int     i,
            j,
            scale,
            gcd,
            C[N][N],
            S[N][N],
            Madj[N][N],
            Tadj[N][N],
            Mdet,
            Tdet;


    Tdet = adjoint (T, Tadj);   /* inverse without division by */
    Mdet = adjoint (M, Madj);   /* determinant of T and M */
    matmult (Madj, Tadj, C);
    matmult (C, M, S);		/* Madj*Tadj*M -> S */
    scale = gcd = Mdet * Tdet;  /* scale factors of both determinants */
    for (i = 0; i < N; i++)	/* find the greatest common */
    {				/* denominator of S and determinants */
		for (j = 0; j < N; j++)
	    	gcd = Gcd (gcd, S[i][j]);
    }
    scale /= gcd;		/* divide everything by gcd to get */
    for (i = 0; i < N; i++)	/* matrix and scale factor in lowest */
    {				/* integer terms possible */
		for (j = 0; j < N; j++)
	    	S[i][j] /= gcd;
    }
    printf ("scale factor = 1/%d  ", scale);
    print_mat ("M=", M, N);     /* display the results */
    print_mat ("T=", T, N);
    print_mat ("S=", S, N);     /* subdivision matrix */
    exit (0);
}
Esempio n. 17
0
MxQuadric3& MxQuadric3::transform(const Mat4& P)
{
    Mat4 Q = homogeneous();
    Mat4 Pa = adjoint(P);

    // Compute:  trans(Pa) * Q * Pa
    // NOTE: Pa is symmetric since Q is symmetric

    Q = Pa * Q * Pa;


    // ??BUG: Should we be transforming the area??
    init(Q, r);

    return *this;
}
Esempio n. 18
0
WTransform WTransform::inverted() const
{
  double det = determinant();

  if (det != 0) {
    WTransform adj = adjoint();

    return WTransform(adj.m11() / det, adj.m12() / det,
		      adj.m21() / det, adj.m22() / det,
		      adj.m31() / det, adj.m32() / det);
  } else {
    LOG_ERROR("inverted(): oops, determinant == 0");

    return *this;
  }
}
BlockedBoundaryOperator<BasisFunctionType, ResultType>
modifiedHelmholtz3dInteriorCalderonProjector(
        const shared_ptr<const Context<BasisFunctionType,ResultType> >& context,
        const shared_ptr<const Space<BasisFunctionType> >& hminusSpace,
        const shared_ptr<const Space<BasisFunctionType> >& hplusSpace,
        KernelType waveNumber,
        const std::string& label,
        bool useInterpolation,
        int interpPtsPerWavelength) {

    typedef BoundaryOperator<BasisFunctionType,ResultType> BdOp;

    shared_ptr<const Space<BasisFunctionType> > internalHplusSpace = hplusSpace->discontinuousSpace(hplusSpace);

    BdOp internalSlp = modifiedHelmholtz3dSingleLayerBoundaryOperator(context,internalHplusSpace,internalHplusSpace,internalHplusSpace,waveNumber,label+"_slp",SYMMETRIC,
                                                                          useInterpolation,interpPtsPerWavelength);

    BdOp dlp = modifiedHelmholtz3dDoubleLayerBoundaryOperator(context,hplusSpace,hplusSpace,hminusSpace,waveNumber,label+"_dlp",NO_SYMMETRY,
                                                                                  useInterpolation,interpPtsPerWavelength);

    BdOp adjDlp = adjoint(dlp);

    BdOp hyp = modifiedHelmholtz3dHypersingularBoundaryOperator(context,hplusSpace,hminusSpace,hplusSpace,waveNumber,label+"_hyp",SYMMETRIC,
                                                                        useInterpolation,interpPtsPerWavelength,internalSlp);

    BdOp idSpaceTransformation1 = identityOperator(context,hminusSpace,internalHplusSpace,internalHplusSpace);
    BdOp idSpaceTransformation2 = identityOperator(context,internalHplusSpace,hplusSpace,hminusSpace);
    BdOp idDouble = identityOperator(context,hplusSpace,hplusSpace,hminusSpace);
    BdOp idAdjDouble = identityOperator(context,hminusSpace,hminusSpace,hplusSpace);

    // Now Assemble the entries of the Calderon Projector

    BlockedOperatorStructure<BasisFunctionType,ResultType> structure;

    structure.setBlock(0,0,.5*idDouble-dlp);
    structure.setBlock(0,1,idSpaceTransformation2*internalSlp*idSpaceTransformation1);
    structure.setBlock(1,0,hyp);
    structure.setBlock(1,1,.5*idAdjDouble+adjDlp);

    return BlockedBoundaryOperator<BasisFunctionType,ResultType>(structure);



}
Esempio n. 20
0
/*!
    Returns an inverted copy of this matrix.

    If the matrix is singular (not invertible), the returned matrix is
    the identity matrix. If \a invertible is valid (i.e. not 0), its
    value is set to true if the matrix is invertible, otherwise it is
    set to false.

    \sa isInvertible()
*/
QTransform QTransform::inverted(bool *invertible) const
{
    qreal det = determinant();
    if (qFuzzyCompare(det, qreal(0.0))) {
        if (invertible)
            *invertible = false;
        return QTransform();
    }
    if (invertible)
        *invertible = true;
    QTransform adjA = adjoint();
    QTransform invert = adjA / det;
    invert = QTransform(invert.m11()/invert.m33(), invert.m12()/invert.m33(), invert.m13()/invert.m33(),
                        invert.m21()/invert.m33(), invert.m22()/invert.m33(), invert.m23()/invert.m33(),
                        invert.m31()/invert.m33(), invert.m32()/invert.m33(), 1);
    // inverting doesn't change the type
    invert.m_type = m_type;
    invert.m_dirty = m_dirty;
    return invert;
}
Esempio n. 21
0
WTransform WTransform::inverted() const
{
  double det = determinant();

  if (det != 0) {
    WTransform adj = adjoint();

    WTransform res(adj.m11() / det, adj.m12() / det,
		   adj.m21() / det, adj.m22() / det,
		   adj.m31() / det, adj.m32() / det);
    if (isJavaScriptBound()) {
      res.assignBinding(*this,
	  WT_CLASS ".gfxUtils.transform_inverted(" + jsRef() + ")");
    }
    return res;
  } else {
    LOG_ERROR("inverted(): oops, determinant == 0");

    return *this;
  }
}
// Returns false if the matrix is not invertible
static bool inverse(const TransformationMatrix::Matrix4& matrix, TransformationMatrix::Matrix4& result)
{
    // Calculate the adjoint matrix
    adjoint(matrix, result);

    // Calculate the 4x4 determinant
    // If the determinant is zero, 
    // then the inverse matrix is not unique.
    double det = determinant4x4(matrix);

    if (fabs(det) < SMALL_NUMBER)
        return false;

    // Scale the adjoint matrix to get the inverse

    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            result[i][j] = result[i][j] / det;

    return true;
}
Esempio n. 23
0
int sa_Inverse3x3(double out[3][3], double in[3][3]) {
    int i, j;
    double det;

    /*  calculate the 3x3 determinant
     *  if the determinant is zero, 
     *  then the inverse matrix is not unique.
     */
    det = sa_Det3x3(in);

    if ( fabs(det) < SA__SMALL_NUMBER)
        return 1;

    /* calculate the adjoint matrix */
    adjoint(out, in);

    /* scale the adjoint matrix to get the inverse */
    for (i = 0; i < 3; i++)
        for(j = 0; j < 3; j++)
		    out[i][j] /= det;
	return 0;
}
Esempio n. 24
0
   Matrix4 Matrix4::Inverse() const
   {
      float minorDeterminant123123 = MinorDeterminant(1, 2, 3, 1, 2, 3);
      float minorDeterminant123023 = MinorDeterminant(1, 2, 3, 0, 2, 3);
      float minorDeterminant123013 = MinorDeterminant(1, 2, 3, 0, 1, 3);
      float minorDeterminant123012 = MinorDeterminant(1, 2, 3, 0, 1, 2);

      float determinant = 
         m[0][0] * minorDeterminant123123 -
         m[0][1] * minorDeterminant123023 +
         m[0][2] * minorDeterminant123013 -
         m[0][3] * minorDeterminant123012;

      Matrix4 adjoint( 
         minorDeterminant123123,
         -MinorDeterminant(0, 2, 3, 1, 2, 3),
         MinorDeterminant(0, 1, 3, 1, 2, 3),
         -MinorDeterminant(0, 1, 2, 1, 2, 3),

         -minorDeterminant123023,
         MinorDeterminant(0, 2, 3, 0, 2, 3),
         -MinorDeterminant(0, 1, 3, 0, 2, 3),
         MinorDeterminant(0, 1, 2, 0, 2, 3),

         minorDeterminant123013,
         -MinorDeterminant(0, 2, 3, 0, 1, 3),
         MinorDeterminant(0, 1, 3, 0, 1, 3),
         -MinorDeterminant(0, 1, 2, 0, 1, 3),

         -minorDeterminant123012,
         MinorDeterminant(0, 2, 3, 0, 1, 2),
         -MinorDeterminant(0, 1, 3, 0, 1, 2),
         MinorDeterminant(0, 1, 2, 0, 1, 2));

      return adjoint * (1.0f / determinant);
   }
Esempio n. 25
0
/* mat1 <-- mat2^-1 */
float mat_inv (Matrix mat1, Matrix mat2)
{
    int i, j;
    float det;

    if (mat1 != mat2) {
        for (i = 0; i < 4; i++)
            for (j = 0; j < 4; j++)
                mat1[i][j] = mat2[i][j];
    }

    det = det4x4 (mat1);

    if (fabs (det) < EPSILON)
        return 0.0;

    adjoint (mat1);

    for (i = 0; i < 4; i++)
        for(j = 0; j < 4; j++)
            mat1[i][j] = mat1[i][j] / det;

    return det;
}
Esempio n. 26
0
Matrix4 Matrix4::inverse() const
{
	return adjoint() * (1.0f / det());
}
Esempio n. 27
0
    void invert(){
		double d = this->det();
		*this = adjoint();
		this->operator *(1/d);
    }
Esempio n. 28
0
/**
 * Returns the inverse of the matrix.  This method makes sure that the
 * matrix can be inverted by verifying that the matrix is not singular.
 * @return A new matrix which is the inverse of the original.
 */
gmMatrix4 gmMatrix4::inverse() const
{
  assert(!isSingular());
  return adjoint() * gmInv(determinant());
}
Esempio n. 29
0
void RG_trace1(int *sign,int cmp[4])
{

  RG_gamma S, T, eta, etap;
  gamma_phase gS, gadjS, gT, geta, getap, gadjeta,gtmp1, gtmp2,gtmp3;
  int rS[4], rT[4],radjS[4],retap[4],radjeta[4],reta[4],r[4];

  S = cmp[0];
  T = cmp[1];

  etap =  int_to_gamma(cmp[2]);
  eta =  int_to_gamma(cmp[3]);

  gamma_to_int(S, rS);
  gamma_to_int(T, rT);
  gamma_to_int(eta, reta);

  gS.g = S;
  gS.sign = 1;
  gT.g = T;
  gT.sign = 1;
  getap.g = etap;
  getap.sign = 1;
  geta.g = eta;
  geta.sign = 1;

  gadjS = adjoint(&gS);
  gamma_to_int(gadjS.g, radjS);
  gtmp1 = gamma_x_gamma(&gadjS, &getap);
  gtmp2 = gamma_x_gamma(&gtmp1, &gT);

  gadjeta = adjoint(&geta);
  gamma_to_int(gadjeta.g, radjeta);
  gtmp3 = gamma_x_gamma(&gtmp2, &gadjeta);

/*
  fprintf(stderr,"%d (%d,%d,%d,%d) == (%d,%d,%d,%d) =  (%d,%d,%d,%d) \n",
	   gadjeta.sign,radjeta[0],radjeta[1],radjeta[2],radjeta[3],
	   reta[0],reta[1],reta[2],reta[3], 
	   rvs[cmp[3]][0],rvs[cmp[3]][1],rvs[cmp[3]][2],rvs[cmp[3]][3] );
*/
/*
fprintf(stderr,"(%d,%d,%d,%d) == (%d,%d,%d,%d) \n",
	   rvs[cmp[2]][0],rvs[cmp[2]][1],rvs[cmp[2]][2],rvs[cmp[2]][3],
	   retap[0],retap[1],retap[2],retap[3]); 
 */ 

  gamma_to_int(etap, retap);
  gamma_to_int(gtmp3.g, r);


 if (gtmp3.g == 0) 
 {
 fprintf(stderr,"Tr[ S^dag(%d,%d,%d,%d) etap(%d,%d,%d,%d) T(%d %d %d %d) etap^dag(%d %d %d %d) ] = %2d(%d,%d,%d,%d)\n",
	   radjS[0],radjS[1],radjS[2],radjS[3],
	   retap[0],retap[1],retap[2],retap[3], 
	   rT[0],rT[1],rT[2],rT[3],
	   radjeta[0],radjeta[1],radjeta[2],radjeta[3],
	   gtmp3.sign,r[0],r[1],r[2],r[3]);

  *sign = gtmp3.sign;
 }
  else
   *sign = 0;

return; 
}
Esempio n. 30
0
inline Term adjoint(const Term & v,const char * s) {
  return Term(v.CoefficientPart(),adjoint(v.MonomialPart(),s));
};