bool isSymmetric(TreeNode* leftRoot, TreeNode* rightRoot) {
     if (leftRoot == nullptr || rightRoot == nullptr)
         return leftRoot == nullptr && rightRoot == nullptr;
     return leftRoot->val == rightRoot->val &&
         isSymmetric(leftRoot->right, rightRoot->left) &&
         isSymmetric(leftRoot->left, rightRoot->right);
     
 }
 bool isSymmetric1(TreeNode *p, TreeNode *q){
     if (p==NULL && q==NULL) return true;
     if (p==NULL || q==NULL) return false;
     
     return (p->val == q->val) &&
             isSymmetric(p->left, q->right) &&
             isSymmetric(p->right, q->left);
 }
예제 #3
0
 bool isSymmetric(TreeNode * left, TreeNode * right)
 {
     if(left == NULL || right == NULL)
         return right == left;
     if(left->val != right ->val)
         return false;
     return isSymmetric(left->left,right->right) && isSymmetric(left->right, right->left);
 }
예제 #4
0
bool isSymmetric (const Node* l, const Node* r)
{
  if (l == 0 && r == 0)
    return true;
  if ( (l == 0 && r != 0) || (l != 0 && r == 0) )
    return false;
  return (l->val == r->val) && isSymmetric (l->left, r->right) && isSymmetric (l->right, r->left);
}
예제 #5
0
   bool isSymmetric(TreeNode *left, TreeNode *right){
       if(left == NULL && right == NULL){   // empty trees are symmetric 
           return true;
       }
       else if(left == NULL || right == NULL){  // empty and non-empty trees are non-symmetric 
           return false;
       }
	   //judge whether left->left right->right, left->right, right->left are symmetric
       return (left->val == right->val)&&isSymmetric(left->left, right->right)&&isSymmetric(left->right, right->left);
   }
예제 #6
0
 bool isSymmetric(TreeNode *left, TreeNode *right)
     {
         if(left == NULL && right == NULL) return true;
         if(left == NULL && right != NULL || left != NULL && right == NULL) 
             return false;
     
         return left->val == right->val 
             && isSymmetric(left->right, right->left) 
             && isSymmetric(left->left, right->right);
     }
예제 #7
0
int main(){
	string trees1[] = {"1","2","2","3","4","4","3"};
	string trees2[] = {"3","9","20","#","#","15","5"};
	TreeNode * root1 = init_complete_binary_tree_from_nodes(trees1,0, sizeof(trees1)/ sizeof(string));
	TreeNode * root2 = init_complete_binary_tree_from_nodes(trees2,0, sizeof(trees2)/ sizeof(string));
	pre_order_travel(root1);
	cout<<endl;
	cout<< isSymmetric(root1)<<endl;
	cout<< isSymmetric(root2)<<endl;
}
예제 #8
0
 bool isSymmetric( TreeNode* left, TreeNode* right )
 {
     if( NULL == left && NULL == right ) return true;
     if( NULL == left && NULL != right ) return false;
     if( NULL != left && NULL == right ) return false;
     if( left->val != right->val ) return false;
     bool flag1 = isSymmetric( left->right, right->left );
     bool flag2 = isSymmetric( left->left, right->right );
     return flag1 && flag2;
 }
예제 #9
0
// Q2 - isSymmetric
bool isSymmetric(TreeNode * a, TreeNode * b)
{
	if (!a && !b) return true;
	else if (!a || !b) return false;
	else
	{
		if (a->val != b->val) return false;
		return isSymmetric(a->left, b->right) && isSymmetric(a->left, b->right);
	}
}
예제 #10
0
    bool isSymmetric(TreeNode* l, TreeNode* r)
    {
        if(!l && !r)
            return true;

        if((!l || !r || (l->val != r->val))
                return false;

                return (isSymmetric(l->left, r->right) && isSymmetric(l->right, r->left));
    }
예제 #11
0
    bool isSymmetric(TreeNode *left_tree, TreeNode *right_tree)
    {
        if(!left_tree && !right_tree)
            return true;
        if(left_tree && !right_tree || !left_tree && right_tree)
            return false;
        if(left_tree->val != right_tree->val)
            return false;

        return isSymmetric(left_tree->left, right_tree->right)
                    && isSymmetric(left_tree->right, right_tree->left);
    }
예제 #12
0
 bool isSymmetric(TreeNode *root) {
     // Note: The Solution object is instantiated only once and is reused by each test case.
     if(root == NULL){    // empty tree is symmetric
         return true;
     }
     return isSymmetric(root->left, root->right);  // whether left and right sub-tree are symmetric
 }
예제 #13
0
  /*! This method employs the static method matrix3x3::jacobi(...)
    to find the eigenvalues and eigenvectors of a symmetric
    matrix. On entry it is checked if the matrix really is
    symmetric: if isSymmetric() returns 'false', an OBError is
    thrown.
 
    \note The jacobi algorithm is should work great for all
    symmetric 3x3 matrices. If you need to find the eigenvectors
    of a non-symmetric matrix, you might want to resort to the
    sophisticated routines of LAPACK.
 
    @param eigenvals a reference to a vector3 where the
    eigenvalues will be stored. The eigenvalues are ordered so
    that eigenvals[0] <= eigenvals[1] <= eigenvals[2].
 
    @return an orthogonal matrix whose ith column is an
    eigenvector for the eigenvalue eigenvals[i]. Here 'orthogonal'
    means that all eigenvectors have length one and are mutually
    orthogonal. The ith eigenvector can thus be conveniently
    accessed by the GetColumn() method, as in the following
    example.
    \code
    // Calculate eigenvectors and -values
    vector3 eigenvals;
    matrix3x3 eigenmatrix = somematrix.findEigenvectorsIfSymmetric(eigenvals);
  
    // Print the 2nd eigenvector
    cout << eigenmatrix.GetColumn(1) << endl;
    \endcode
    With these conventions, a matrix is diagonalized in the following way:
    \code
    // Diagonalize the matrix
    matrix3x3 diagonalMatrix = eigenmatrix.inverse() * somematrix * eigenmatrix;
    \endcode
  
  */
  matrix3x3 matrix3x3::findEigenvectorsIfSymmetric(vector3 &eigenvals) const
#ifdef OB_OLD_MATH_CHECKS
  throw(OBError)
#endif
  {
    matrix3x3 result;

#ifdef OB_OLD_MATH_CHECKS
    if (!isSymmetric())
      {
        OBError er("matrix3x3::findEigenvectorsIfSymmetric(vector3 &eigenvals) const throw(OBError)",
                   "The method was called on a matrix that was not symmetric, i.e. where isSymetric() == false.",
                   "This is a runtime or a programming error in your application.");
        throw er;
      }
#endif

    double d[3];
    matrix3x3 copyOfThis = *this;

    jacobi(3, copyOfThis.ele[0], d, result.ele[0]);
    eigenvals.Set(d);

    return result;
  }
예제 #14
0
int main(){

	char t[]="{1,2,3,#,4,5,#,6,7,#,8}";
	char t2[]="{1,2,3,#,4,5,#,6,7,8,2}";
	struct TreeNode *r = make_tree(t);
	struct TreeNode *r2 = make_tree(t2);

	if(isSameTree(r,r2)){
		puts("same Tree");
	} else {
		puts("diff Tree");
	}
	printf("after t%s\n",t);

	char st[]="{1,2,2,3,4,4,3}";
	struct TreeNode *sr = make_tree(st);

	simple_inorder(sr);
	puts("end ino");
	simple_preorder(sr);
	puts("end pre");
	if(isSymmetric(sr)){
		puts("symmetric Tree");
	} else {
		puts("no sym Tree");
	}
}
예제 #15
0
    bool isSymmetric(TreeNode *root) {
        if (root)
        {
            return isSymmetric(root->right, root->left);
        }

        return true;
    }
예제 #16
0
 bool isSymmetric(TreeNode *root) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     
     if (!root) return true;
     
     
     return isSymmetric(root->left, root->right);
 }
예제 #17
0
파일: sol2.cpp 프로젝트: wddabc/leetcode
int main(int argc, const char* argv[])
{
	TreeNode *root = new TreeNode(2);
	root->left = new TreeNode(3);
	root->right = new TreeNode(3);
	root->left->left = new TreeNode(4);
	root->left->right = new TreeNode(5);
	root->right->left = new TreeNode(5);
	cout << isSymmetric(root) << endl;
	return 0;
}
예제 #18
0
    bool isSymmetric(TreeNode *r1, TreeNode *r2)
    {
        if (!r1 && !r2)
        {
            return true;
        }
        else if (r1 && r2)
        {
            if (r1->val != r2->val)
            {
                return false;
            }

            return isSymmetric(r1->right, r2->left) && isSymmetric(r1->left, r2->right);
        }
        else
        {
            return false;
        }
    }
예제 #19
0
파일: isSymetric.cpp 프로젝트: hmofrad/etc
int main(int argc, char** argv) {
    TreeNode* p = new TreeNode(1);
    p->left = new TreeNode(2);
    p->right = new TreeNode(2);
    p->left->left = new TreeNode(3);
    p->left->right = new TreeNode(4);
    p->right->left = new TreeNode(4);
    p->right->right = new TreeNode(3);
    
    printf("isSymmetric? %d\n", isSymmetric(p));
    return(0);
}
예제 #20
0
void main()
{
int a[4][4];
int i,j,k;
for(i=0;i<4;i++)
{
    for(j=0;j<4;j++)
    {
      scanf("%d",&a[i][j]);
    }
}
    k=isSymmetric(a,4);
    printf("%4d",k);
    getch();
}
예제 #21
0
void
LinearOperator::applyAdjoint(
    SAMRAIVectorReal<NDIM,double>& x,
    SAMRAIVectorReal<NDIM,double>& y)
{
    if (isSymmetric())
    {
        apply(x,y);
    }
    else
    {
        TBOX_ERROR("LinearOperator::applyAdjoint():\n"
                   << "  no adjoint operation defined for this linear operator" << std::endl);
    }
    return;
}// applyAdjoint
예제 #22
0
//------------------------------------------------------------------------------
// Returns pre-ref'd pointers to the Cholesky (pL) of 'this' matrix and its
// transpose (pU). Function returns false if the matrix can not be Cholesky-ized
//------------------------------------------------------------------------------
bool Matrix::getCholesky(Matrix* const pL, Matrix* const pU) const
{
   //-------------------------------------------------------
   // initial compatibility and error checks
   //-------------------------------------------------------
   if (!isSymmetric()) return false;

   //-------------------------------------------------------
   // calculate the lower Cholesky matrix
   //-------------------------------------------------------
   const unsigned int N = rows;
   for (unsigned int j = 0; j < N; j++) {
      for (unsigned int i = j; i < N; i++) {
         (*pL)(i,j) = (*this)(i,j);
         for (unsigned int k=0; k<j; k++) {
            (*pL)(i,j) -= (*pL)(i,k) * (*pL)(j,k);
         }

         if (i == j) {
            double x = (*pL)(i,j);
            if (x < 0.0) {
               std::cout << "Cholesky failed. Matrix is not positive definite." << std::endl;
               return false;
            }
            (*pL)(i,j) = std::sqrt(x);
         }
         else {
            double x = (*pL)(j,j);
            double eps = 1.0e-12;
            if (std::fabs(x) < eps) {
               // Note: excessive roundoff error for some reason - LDB
               std::cout << "Cholesky failed. Division by very small number." << std::endl;
               return false;
            }
            (*pL)(i,j) /= x;
         }
      }
   }

   //-------------------------------------------------------
   // upper Cholesky matrix is the transpose of lower matrix
   //-------------------------------------------------------
   *pU = *pL;
   pU->transpose();

   return true;
}
예제 #23
0
int main() {
	TreeNode *root = new TreeNode(1);
	TreeNode *L1 = new TreeNode(2);
	TreeNode *R1 = new TreeNode(2);
	root->left = L1;
	root->right = R1;
	L1->left = new TreeNode(3);
	R1->right = new TreeNode(3);


	if (isSymmetric(root))
		cout << "ok" << endl;
	else
		cout << "no" << endl;

	int n;
	cin >> n;
	return 0;
}
예제 #24
0
파일: array.hpp 프로젝트: ArduPilot/uavcan
 PackingMode detectOptimalPackingMode() const
 {
     if (areAllElementsNan())
     {
         return PackingModeEmpty;
     }
     if (isScalar())
     {
         return PackingModeScalar;
     }
     if (isDiagonal())
     {
         return PackingModeDiagonal;
     }
     if (isSymmetric())
     {
         return PackingModeSymmetric;
     }
     return PackingModeFull;
 }
예제 #25
0
bool
RankFourTensor::isIsotropic() const
{
  // prerequisite is symmetry
  if (!isSymmetric())
    return false;

  // inspect shear components
  const Real mu = (*this)(0, 1, 0, 1);
  // ...diagonal
  if ((*this)(1, 2, 1, 2) != mu || (*this)(2, 0, 2, 0) != mu)
    return false;
  // ...off-diagonal
  if ((*this)(2, 0, 1, 2) != 0.0 || (*this)(0, 1, 1, 2) != 0.0 || (*this)(0, 1, 2, 0) != 0.0)
    return false;

  // off diagonal blocks in Voigt
  unsigned int i1 = 0;
  for (unsigned int i = 0; i < N; ++i)
  {
    for (unsigned int j = 0; j < N; ++j)
      if (_vals[i1 + ((j + 1) % N) * N + (j + 2) % N] != 0.0)
        return false;
    i1 += N3 + N2;
  }

  // top left block
  const Real K1 = (*this)(0, 0, 0, 0);
  const Real K2 = (*this)(0, 0, 1, 1);
  if (!MooseUtils::relativeFuzzyEqual(K1 - 4.0 * mu / 3.0, K2 + 2.0 * mu / 3.0))
    return false;
  if ((*this)(1, 1, 1, 1) != K1 || (*this)(2, 2, 2, 2) != K1)
    return false;
  for (unsigned int i = 1; i < N; ++i)
    for (unsigned int j = 0; j < i; ++j)
      if ((*this)(i, i, j, j) != K2)
        return false;

  return true;
}
예제 #26
0
inline
IntervalWrapper<BoostInterval> IntervalWrapper<BoostInterval>::getMedianInterval() const
{
	if (isEmpty())
		return BoostInterval::empty();

 	if (isSymmetric())  // symmetric case handles [-oo, +oo]
 		return 0.0;

 	if (getLb() == -std::numeric_limits<Real>::infinity())
 		return -std::numeric_limits<Real>::max();

	if (getUb() == std::numeric_limits<Real>::infinity())
		return std::numeric_limits<Real>::max();

	Rounding rnd;
	rnd.setDownward();
	Real lb = .5*(getLb()+getUb());
	rnd.setUpward();
	Real ub = .5*(getLb()+getUb());
	return BoostInterval(lb,ub);
}
예제 #27
0
Foam::eigenSolver::eigenSolver
(
    const scalarSquareMatrix& A
)
:
    eigenvaluesRe_(A.n()),
    eigenvaluesIm_(A.n()),
    eigenvectors_(A.n(), A.n()),
    H_(),
    n_(A.n())
{
    if (isSymmetric(A))
    {
        eigenvectors_ = A;
        tridiagonaliseSymmMatrix();
        symmTridiagQL();
    }
    else
    {
        H_ = A;
        Hessenberg();
        realSchur();
    }
}
예제 #28
0
types::Function::ReturnValue sci_spec(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    double* pDataA          = NULL;
    double* pDataB          = NULL;
    bool symmetric          = FALSE;
    int iRet                = 0;

    if (in.size() != 1 && in.size() != 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "spec", 1, 2);
        return types::Function::Error;
    }

    if (_iRetCount > 2 * in.size())
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), "spec", 1, 2 * in.size());
        return types::Function::Error;
    }

    if (in[0]->isDouble() == false)
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_spec";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    types::Double* in0 = in[0]->getAs<types::Double>();

    if (in0->getCols() != in0->getRows())
    {
        Scierror(20, _("%s: Wrong type for input argument #%d: Square matrix expected.\n"), "spec", 1);
        return types::Function::Error;
    }

    if (in0->getRows() == -1 || in0->getCols() == -1) // manage eye case
    {
        Scierror(271, _("%s: Size varying argument a*eye(), (arg %d) not allowed here.\n"), "spec", 1);
        return types::Function::Error;
    }

    if (in0->getCols() == 0 || in0->getRows() == 0) // size null
    {
        out.push_back(types::Double::Empty());
        for (int i = 1; i < _iRetCount; i++)
        {
            out.push_back(types::Double::Empty());
        }
        return types::Function::OK;
    }

    types::Double* pDblA = in0->clone()->getAs<types::Double>();

    if (in.size() == 1)
    {
        types::Double* pDblEigenValues  = NULL;
        types::Double* pDblEigenVectors = NULL;

        if (pDblA->isComplex())
        {
            pDataA = (double*)oGetDoubleComplexFromPointer(pDblA->getReal(), pDblA->getImg(), pDblA->getSize());
            if (!pDataA)
            {
                pDblA->killMe();
                Scierror(999, _("%s: Cannot allocate more memory.\n"), "spec");
                return types::Function::Error;
            }
        }
        else
        {
            pDataA = pDblA->getReal();
        }

        int totalSize = pDblA->getSize();
        if ((pDblA->isComplex() ? C2F(vfiniteComplex)(&totalSize, (doublecomplex*)pDataA) : C2F(vfinite)(&totalSize, pDataA)) == false)
        {
            if (pDblA->isComplex())
            {
                vFreeDoubleComplexFromPointer((doublecomplex*)pDataA);
            }
            pDblA->killMe();
            Scierror(264, _("%s: Wrong value for input argument %d: Must not contain NaN or Inf.\n"), "spec", 1);
            return types::Function::Error;
        }

        symmetric = isSymmetric(pDblA->getReal(), pDblA->getImg(), pDblA->isComplex(), pDblA->getRows(), pDblA->getCols()) == 1;
        int eigenValuesCols = (_iRetCount == 1) ? 1 : pDblA->getCols();

        if (symmetric)
        {
            pDblEigenValues = new types::Double(pDblA->getCols(), eigenValuesCols);
            if (_iRetCount == 2)
            {
                pDblEigenVectors = new types::Double(pDblA->getCols(), pDblA->getCols(), pDblA->isComplex());
            }
        }
        else
        {
            pDblEigenValues  = new types::Double(pDblA->getCols(), eigenValuesCols, true);
            if (_iRetCount == 2)
            {
                pDblEigenVectors = new types::Double(pDblA->getCols(), pDblA->getCols(), true);
            }
        }

        if (pDblA->isComplex())
        {
            if (symmetric)
            {
                iRet = iEigen1ComplexSymmetricM((doublecomplex*)pDataA, pDblA->getCols(), (_iRetCount == 2), pDblEigenValues->getReal());

                if (iRet < 0)
                {
                    vFreeDoubleComplexFromPointer((doublecomplex*)pDataA);
                    pDblA->killMe();
                    Scierror(998, _("%s: On entry to ZGEEV parameter number  3 had an illegal value (lapack library problem).\n"), "spec", iRet);
                    return types::Function::Error;
                }

                if (iRet > 0)
                {
                    vFreeDoubleComplexFromPointer((doublecomplex*)pDataA);
                    pDblA->killMe();
                    Scierror(24, _("%s: Convergence problem, %d off-diagonal elements of an intermediate tridiagonal form did not converge to zero.\n"), "spec", iRet);
                    return types::Function::Error;
                }

                if (_iRetCount == 2)
                {
                    vGetPointerFromDoubleComplex((doublecomplex*)pDataA, pDblA->getSize() , pDblEigenVectors->getReal(), pDblEigenVectors->getImg());
                    vFreeDoubleComplexFromPointer((doublecomplex*)pDataA);
                    expandToDiagonalOfMatrix(pDblEigenValues->getReal(), pDblA->getCols());
                    out.push_back(pDblEigenVectors);
                }
                out.push_back(pDblEigenValues);
            }
            else // not symmetric
            {
                doublecomplex* pEigenValues = (doublecomplex*)MALLOC(pDblA->getCols() * sizeof(doublecomplex));
                doublecomplex* pEigenVectors = pDblEigenVectors ? (doublecomplex*)MALLOC(sizeof(doublecomplex) * pDblA->getSize()) : NULL;
                iRet = iEigen1ComplexM((doublecomplex*)pDataA, pDblA->getCols(), pEigenValues, pEigenVectors);
                vFreeDoubleComplexFromPointer((doublecomplex*)pDataA);
                if (iRet < 0)
                {
                    pDblA->killMe();
                    Scierror(998, _("%s: On entry to ZHEEV parameter number  3 had an illegal value (lapack library problem).\n"), "spec", iRet);
                    return types::Function::Error;
                }

                if (iRet > 0)
                {
                    pDblA->killMe();
                    Scierror(24, _("%s: The QR algorithm failed to compute all the eigenvalues, and no eigenvectors have been computed. Elements and %d+1:N of W contain eigenvalues which have converged.\n"), "spec", iRet);
                    return types::Function::Error;
                }

                if (_iRetCount == 2)
                {
                    expandZToDiagonalOfCMatrix(pEigenValues, pDblA->getCols(), pDblEigenValues->getReal(), pDblEigenValues->getImg());
                    vGetPointerFromDoubleComplex(pEigenVectors, pDblA->getSize(), pDblEigenVectors->getReal(), pDblEigenVectors->getImg());

                    FREE(pEigenVectors);
                    out.push_back(pDblEigenVectors);
                }
                else
                {
                    vGetPointerFromDoubleComplex(pEigenValues, pDblA->getCols(), pDblEigenValues->getReal(), pDblEigenValues->getImg());
                }
                out.push_back(pDblEigenValues);
                FREE(pEigenValues);
                pDblA->killMe();
            }
        }
        else // real
        {
            if (symmetric)
            {
                iRet = iEigen1RealSymmetricM(pDataA, pDblA->getCols(), (_iRetCount == 2), pDblEigenValues->getReal());
                if (iRet < 0)
                {
                    pDblA->killMe();
                    Scierror(998, _("%s: On entry to ZGEEV parameter number  3 had an illegal value (lapack library problem).\n"), "spec", iRet);
                    return types::Function::Error;
                }

                if (iRet > 0)
                {
                    pDblA->killMe();
                    Scierror(24, _("%s: Convergence problem, %d off-diagonal elements of an intermediate tridiagonal form did not converge to zero.\n"), "spec", iRet);
                    return types::Function::Error;
                }

                if (_iRetCount == 2)
                {
                    expandToDiagonalOfMatrix(pDblEigenValues->getReal(), pDblA->getCols());
                    out.push_back(pDblA);
                }
                else
                {
                    pDblA->killMe();
                }

                out.push_back(pDblEigenValues);
            }
            else // not symmetric
            {
                iRet = iEigen1RealM(pDataA, pDblA->getCols(), pDblEigenValues->getReal(), pDblEigenValues->getImg(), pDblEigenVectors ? pDblEigenVectors->getReal() : NULL, pDblEigenVectors ? pDblEigenVectors->getImg() : NULL);

                if (iRet < 0)
                {
                    pDblA->killMe();
                    Scierror(998, _("%s: On entry to ZHEEV parameter number  3 had an illegal value (lapack library problem).\n"), "spec", iRet);
                    return types::Function::Error;
                }

                if (iRet > 0)
                {
                    pDblA->killMe();
                    Scierror(24, _("%s: The QR algorithm failed to compute all the eigenvalues, and no eigenvectors have been computed. Elements and %d+1:N of WR and WI contain eigenvalues which have converged.\n"), "spec", iRet);
                    return types::Function::Error;
                }

                if (_iRetCount == 2)
                {
                    expandToDiagonalOfMatrix(pDblEigenValues->getReal(), pDblA->getCols());
                    expandToDiagonalOfMatrix(pDblEigenValues->getImg(), pDblA->getCols());
                    out.push_back(pDblEigenVectors);
                }

                out.push_back(pDblEigenValues);
                pDblA->killMe();
            }
        }

        return types::Function::OK;
    }

    if (in.size() == 2)
    {
        types::Double* pDblL            = NULL;
        types::Double* pDblR            = NULL;
        types::Double* pDblBeta         = NULL;
        types::Double* pDblAlpha        = NULL;
        doublecomplex* pL               = NULL;
        doublecomplex* pR               = NULL;
        doublecomplex* pBeta            = NULL;
        doublecomplex* pAlpha           = NULL;
        bool bIsComplex                 = false;

        if (in[1]->isDouble() == false)
        {
            std::wstring wstFuncName = L"%" + in[1]->getShortTypeStr() + L"_spec";
            return Overload::call(wstFuncName, in, _iRetCount, out);
        }

        types::Double* in1 = in[1]->getAs<types::Double>();

        if (in1->getCols() != in1->getRows())
        {
            Scierror(20, _("%s: Wrong type for input argument #%d: Square matrix expected.\n"), "spec", 2);
            return types::Function::Error;
        }

        if (pDblA->getRows() != in1->getRows() && pDblA->getCols() != in1->getCols())
        {
            pDblA->killMe();
            Scierror(999, _("%s: Arguments %d and %d must have equal dimensions.\n"), "spec", 1, 2);
            return types::Function::Error;
        }

        //chekc if A and B are real complex or with imag part at 0
        if (isNoZeroImag(pDblA) == false && isNoZeroImag(in1) == false)
        {
            //view A and B as real matrix
            bIsComplex = false;
        }
        else
        {
            bIsComplex = pDblA->isComplex() || in1->isComplex();
        }

        types::Double* pDblB = in1->clone()->getAs<types::Double>();
        if (bIsComplex)
        {
            if (pDblA->isComplex() == false)
            {
                pDblA->setComplex(true);
            }

            if (pDblB->isComplex() == false)
            {
                pDblB->setComplex(true);
            }

            pDataA = (double*)oGetDoubleComplexFromPointer(pDblA->getReal(), pDblA->getImg(), pDblA->getSize());
            pDataB = (double*)oGetDoubleComplexFromPointer(pDblB->getReal(), pDblB->getImg(), pDblB->getSize());

            if (!pDataA || !pDataB)
            {
                delete pDataA;
                delete pDataB;
                Scierror(999, _("%s: Cannot allocate more memory.\n"), "spec");
                return types::Function::Error;
            }
        }
        else
        {
            pDataA = pDblA->getReal();
            pDataB = pDblB->getReal();
        }

        int totalSize = pDblA->getSize();

        if ((pDblA->isComplex() ? C2F(vfiniteComplex)(&totalSize, (doublecomplex*)pDataA) : C2F(vfinite)(&totalSize, pDataA)) == false)
        {
            pDblA->killMe();
            pDblB->killMe();
            Scierror(264, _("%s: Wrong value for input argument %d: Must not contain NaN or Inf.\n"), "spec", 1);
            return types::Function::Error;
        }

        if ((pDblB->isComplex() ? C2F(vfiniteComplex)(&totalSize, (doublecomplex*)pDataB) : C2F(vfinite)(&totalSize, pDataB)) == false)
        {
            pDblA->killMe();
            pDblB->killMe();
            Scierror(264, _("%s: Wrong value for input argument %d: Must not contain NaN or Inf.\n"), "spec", 2);
            return types::Function::Error;
        }

        switch (_iRetCount)
        {
            case 4:
            {
                pDblL = new types::Double(pDblA->getRows(), pDblA->getCols(), true);
                if (bIsComplex)
                {
                    pL = (doublecomplex*)MALLOC(pDblA->getSize() * sizeof(doublecomplex));
                }
            }
            case 3:
            {
                pDblR = new types::Double(pDblA->getRows(), pDblA->getCols(), true);
                if (bIsComplex)
                {
                    pR = (doublecomplex*)MALLOC(pDblA->getSize() * sizeof(doublecomplex));
                }
            }
            case 2:
            {
                if (bIsComplex)
                {
                    pBeta = (doublecomplex*)MALLOC(pDblA->getCols() * sizeof(doublecomplex));
                }
                pDblBeta = new types::Double(pDblA->getCols(), 1, pBeta ? true : false);
            }
            default : // case 1:
            {
                if (bIsComplex)
                {
                    pAlpha = (doublecomplex*)MALLOC(pDblA->getCols() * sizeof(doublecomplex));
                }
                pDblAlpha = new types::Double(pDblA->getCols(), 1, true);
            }
        }

        if (bIsComplex)
        {
            iRet = iEigen2ComplexM((doublecomplex*)pDataA, (doublecomplex*)pDataB, pDblA->getCols(), pAlpha, pBeta, pR, pL);
        }
        else
        {
            iRet = iEigen2RealM(    pDataA, pDataB, pDblA->getCols(),
                                    pDblAlpha->getReal(), pDblAlpha->getImg(),
                                    pDblBeta ? pDblBeta->getReal()  : NULL,
                                    pDblR    ? pDblR->getReal()     : NULL,
                                    pDblR    ? pDblR->getImg()      : NULL,
                                    pDblL    ? pDblL->getReal()     : NULL,
                                    pDblL    ? pDblL->getImg()      : NULL);
        }

        if (iRet > 0)
        {
            sciprint(_("Warning :\n"));
            sciprint(_("Non convergence in the QZ algorithm.\n"));
            sciprint(_("The top %d  x %d blocks may not be in generalized Schur form.\n"), iRet);
        }

        if (iRet < 0)
        {
            pDblA->killMe();
            pDblB->killMe();
            Scierror(998, _("%s: On entry to ZHEEV parameter number  3 had an illegal value (lapack library problem).\n"), "spec", iRet);
            return types::Function::Error;
        }

        if (iRet > 0)
        {
            if (bIsComplex)
            {
                if (iRet <= pDblA->getCols())
                {
                    Scierror(24, _("%s: The QZ iteration failed in DGGEV.\n"), "spec");
                }
                else
                {
                    if (iRet == pDblA->getCols() + 1)
                    {
                        Scierror(999, _("%s: Other than QZ iteration failed in DHGEQZ.\n"), "spec");
                    }
                    if (iRet == pDblA->getCols() + 2)
                    {
                        Scierror(999, _("%s: Error return from DTGEVC.\n"), "spec");
                    }
                }
            }
            else
            {
                Scierror(24, _("%s: The QR algorithm failed to compute all the eigenvalues, and no eigenvectors have been computed. Elements and %d+1:N of W contain eigenvalues which have converged.\n"), "spec", iRet);
            }

            pDblA->killMe();
            pDblB->killMe();
            if (pDataA)
            {
                vFreeDoubleComplexFromPointer((doublecomplex*)pDataA);
            }

            if (pDataB)
            {
                vFreeDoubleComplexFromPointer((doublecomplex*)pDataB);
            }
            return types::Function::Error;
        }

        if (bIsComplex)
        {
            switch (_iRetCount)
            {
                case 4:
                    vGetPointerFromDoubleComplex(pL, pDblA->getSize(), pDblL->getReal(), pDblL->getImg());
                case 3:
                    vGetPointerFromDoubleComplex(pR, pDblA->getSize(), pDblR->getReal(), pDblR->getImg());
                case 2:
                    vGetPointerFromDoubleComplex(pBeta, pDblA->getCols(), pDblBeta->getReal(), pDblBeta->getImg());
                default : // case 1:
                    vGetPointerFromDoubleComplex(pAlpha, pDblA->getCols(), pDblAlpha->getReal(), pDblAlpha->getImg());
            }
        }

        switch (_iRetCount)
        {
            case 1:
            {
                out.push_back(pDblAlpha);
                break;
            }
            case 2:
            {
                out.push_back(pDblAlpha);
                out.push_back(pDblBeta);
                break;
            }
            case 3:
            {
                out.push_back(pDblAlpha);
                out.push_back(pDblBeta);
                out.push_back(pDblR);
                break;
            }
            case 4:
            {
                out.push_back(pDblAlpha);
                out.push_back(pDblBeta);
                out.push_back(pDblL);
                out.push_back(pDblR);
            }
        }

        if (pAlpha)
        {
            vFreeDoubleComplexFromPointer(pAlpha);
        }
        if (pBeta)
        {
            vFreeDoubleComplexFromPointer(pBeta);
        }
        if (pL)
        {
            vFreeDoubleComplexFromPointer(pL);
        }
        if (pR)
        {
            vFreeDoubleComplexFromPointer(pR);
        }
        if (bIsComplex && pDblB->isComplex())
        {
            vFreeDoubleComplexFromPointer((doublecomplex*)pDataB);
        }
        pDblB->killMe();

    } // if(in.size() == 2)

    if (pDblA->isComplex())
    {
        vFreeDoubleComplexFromPointer((doublecomplex*)pDataA);
    }

    return types::Function::OK;
}
예제 #29
0
 bool isSymmetric(TreeNode* root) {
     if(root==NULL)
         return true;
     return isSymmetric(root->left,root->right);
 }
예제 #30
0
 bool isSymmetric(TreeNode *root) {
     return isSymmetric(root, root);
 }