Example #1
0
/*
 * Description not yet available.
 * \param
 */
void dvdv_dot(void)
{
    verify_identifier_string("aaaa");
    double dftmp=restore_prevariable_derivative();
    dvar_vector_position v2pos=restore_dvar_vector_position();
    dvector cv2=restore_dvar_vector_value(v2pos);
    dvar_vector_position v1pos=restore_dvar_vector_position();
    dvector cv1=restore_dvar_vector_value(v1pos);
    verify_identifier_string("bbbb");
    dvector dfv1(cv1.indexmin(),cv1.indexmax());
    dvector dfv2(cv2.indexmin(),cv2.indexmax());
#ifdef OPT_LIB
    double * pdf1=&dfv1(cv1.indexmin());
    double * pdf1m=&dfv1(cv1.indexmax());
    double * pdf2=&dfv2(cv1.indexmin());
    double * pc1=&cv1(cv1.indexmin());
    double * pc2=&cv2(cv1.indexmin());
    do
    {
        *pdf1++ = dftmp * *pc2++;
        *pdf2++ = dftmp * *pc1++;
    }
    while (pdf1<=pdf1m);
#else
    for (int i=cv1.indexmin(); i<=cv1.indexmax(); i++)
    {
        //tmp+=cv1(i)*cv2(i);
        dfv1(i)=dftmp*cv2.elem(i);
        dfv2(i)=dftmp*cv1.elem(i);
    }
#endif
    dfv1.save_dvector_derivatives(v1pos);
    dfv2.save_dvector_derivatives(v2pos);
}
Example #2
0
void test(const std::string& inName)
{
	cybozu::Mmap m(inName);
	const uint64_t *blk = reinterpret_cast<const uint64_t*>(m.get());
	cybozu::CSucVector cv2(blk, m.size() * 8);
	cybozu::CSucVector cv;
	{
		std::stringstream ss;
		cv2.save(ss);
		cv.load(ss);
		const int inSize = (int)m.size();
		const int outSize = (int)ss.str().size();
		printf("rate = %.2f%% %d / %d\n", outSize * 100.0 / inSize, outSize, inSize);
	}
	cybozu::BitVector bv;
	bv.resize(m.size() * 8);
	for (size_t i = 0, n = bv.size(); i < n; i++) {
		if (cv.get(i)) bv.set(i);
	}
	const uint64_t *p = bv.getBlock();
	for (size_t i = 0, n = bv.getBlockSize(); i < n; i++) {
		if (p[i] != blk[i]) {
			printf("err i=%d %llx %llx\n", (int)i, (long long)blk[i], (long long)p[i]);
			exit(1);
		}
	}
}
Example #3
0
double _AngleBetween(VECTOR3D v1, VECTOR3D v2)					//retrun 0 to 180 degree
{
	CVector3D cv1(v1), cv2(v2);
	cv1.Normalize();
	cv2.Normalize();

	return acos(max(-1.0, min(1.0, cv1 | cv2)));
}
Example #4
0
float _AngleBetween(VECTOR3D v1,VECTOR3D v2)
{
	if(_IsParallel(v1,v2))	return 0;
	
	CVector3D cv1(v1),cv2(v2);
	CVector3D flag = cv2*cv1;
	//TRACE( "\nflag %f", flag.dz );
	if( flag.dz >0 )
		return acos((cv1|cv2.GetNormal())/cv1.GetLength());
	else
		return -acos((cv1|cv2.GetNormal())/cv1.GetLength());
}
Example #5
0
void testCopyConstructor(){

    ContentValue cv1;

    // INT 32
    int value32 = 1;
    std::string key = "one";

    cv1.put(key, value32);

    // INT 64
    int64_t value64 = 423425242;
    key = "int64";
    cv1.put(key, value64);

    // Double
    double dvalue = 3.5;
    key = "double";
    cv1.put(key, dvalue);

    // BLOB
    uint32_t data_len = 45;
    char* src = new char[data_len];

    memset(src, '$', data_len);
    key = "data";
    cv1.put(key, data_len, src);


    delete[] src;

    // STRING
    std::string strVal = "whoo";
    key = "string";
    cv1.put(key, strVal);

    // BOOL
    bool mefalse = false;
    key = "bool";
    cv1.put(key, mefalse);

    ContentValue cv2(cv1);

    CHECK(cv1 == cv2);

    cv1.clear();
    cv2.clear();

    REPORT("testCopyConstructor()");
}
Example #6
0
double _AngleBetween(VECTOR3D v1, VECTOR3D v2, VECTOR3D normal)	//if 0, return 360
{
	CVector3D cv1(v1), cv2(v2), norm(normal);
	// 	if ( _IsParallel(cv1,cv2) )
	// 	{
	// 		if ((cv1|cv2) <= 0)
	// 		{
	// 			return 0;
	// 		}
	// 	}
	cv1.Normalize();
	cv2.Normalize();
	if ( ( ( norm*cv1 ) | cv2 ) > 0 )
		return acos(max(-1.0, min(1.0, cv1 | cv2)));
	else
		return 2 * PI - acos(max(-1.0, min(1.0, cv1 | cv2)));

}
TEST(CircularValue_NegativeRange, Init)
{
    {
        cv_type cv;
        EXPECT_EQ(-100, cv);
    }
    {
        cv_type cv = cv_type::value_type(-20);
        EXPECT_EQ(-20, cv);
    }
    {
        cv_type cv(-20);
        EXPECT_EQ(-20, cv);

        cv_type cv2(cv);
        EXPECT_EQ(-20, cv2);
    }
}
Example #8
0
/*
 * Read in the MBR of the disk.  If it is boot0, then use the version to
 * read in all of it if necessary.  Use pointers to return a malloc'd
 * buffer containing the MBR and then return its size.
 */
static int
read_mbr(const char *disk, u_int8_t **mbr, int check_version)
{
    u_int8_t buf[MBRSIZE];
    int mbr_size, fd;
    int ver;
    ssize_t n;

    if ((fd = open(disk, O_RDONLY)) == -1)
        err(1, "open %s", disk);
    if ((n = read(fd, buf, MBRSIZE)) == -1)
        err(1, "read %s", disk);
    if (n != MBRSIZE)
        errx(1, "%s: short read", disk);
    if (cv2(buf + OFF_MAGIC) != 0xaa55)
        errx(1, "%s: bad magic", disk);

    if (! (ver = boot0bs(buf))) {
	if (check_version)
	    errx(1, "%s: unknown or incompatible boot code", disk);
    } else if (boot0version(buf) == 0x101) {
	mbr_size = 1024;
	if ((*mbr = malloc(mbr_size)) == NULL)
	    errx(1, "%s: unable to allocate read buffer", disk);
	if (lseek(fd, 0, SEEK_SET) == -1 ||
	    (n = read(fd, *mbr, mbr_size)) == -1)
	    err(1, "%s", disk);
	if (n != mbr_size)
	    errx(1, "%s: short read", disk);
	close(fd);
	return (mbr_size);
    }
    *mbr = malloc(sizeof(buf));
    if (*mbr == NULL)
	errx(1, "%s: unable to allocate MBR buffer", disk);
    memcpy(*mbr, buf, sizeof(buf));
    close(fd);

    return sizeof(buf);
}
Example #9
0
BOOL _IsOrthogonal(VECTOR3D v1, VECTOR3D v2)
{
	CVector3D cv1(v1), cv2(v2);
	return IS_ZERO(cv1 | cv2);
}
Example #10
0
BOOL _IsParallel(VECTOR3D v1, VECTOR3D v2)
{
	CVector3D cv1(v1), cv2(v2);
	return IS_ZERO(( cv1*cv2 ) | ( cv1*cv2 ));
}
Example #11
0
void drive_operation()
{

    // Uint64 tests

    CQLValue a1(Uint64(10));
    CQLValue a2(Uint64(15));
    CQLValue a3(Uint64(25));
    CQLValue a4(Uint64(30));
    CQLValue a5(Uint64(150));

    PEGASUS_TEST_ASSERT(a1 != a2);
    PEGASUS_TEST_ASSERT(a5 == a5);
    PEGASUS_TEST_ASSERT(a1 < a2);
    PEGASUS_TEST_ASSERT(a2 >= a1);
    PEGASUS_TEST_ASSERT(a1 <= a2);
    PEGASUS_TEST_ASSERT(a2 > a1);

    // Sint64 tests

    CQLValue b1(Sint64(10));
    CQLValue b2(Sint64(15));
    CQLValue b3(Sint64(25));
    CQLValue b4(Sint64(30));
    CQLValue b5(Sint64(150));

    PEGASUS_TEST_ASSERT(b1 != b2);
    PEGASUS_TEST_ASSERT(b5 == b5);
    PEGASUS_TEST_ASSERT(b1 < b2);
    PEGASUS_TEST_ASSERT(b2 >= b1);
    PEGASUS_TEST_ASSERT(b1 <= b2);
    PEGASUS_TEST_ASSERT(b2 > b1);

    // Real64 tests

    CQLValue c1(Real64(10.00));
    CQLValue c2(Real64(15.00));
    CQLValue c3(Real64(25.00));
    CQLValue c4(Real64(30.00));
    CQLValue c5(Real64(150.00));

    PEGASUS_TEST_ASSERT(c1 != c2);
    PEGASUS_TEST_ASSERT(c5 == c5);
    PEGASUS_TEST_ASSERT(c1 < c2);
    PEGASUS_TEST_ASSERT(c2 >= c1);
    PEGASUS_TEST_ASSERT(c1 <= c2);
    PEGASUS_TEST_ASSERT(c2 > c1);

    // Misc
    PEGASUS_TEST_ASSERT(a1 == b1);
    PEGASUS_TEST_ASSERT(a1 == c1);
    PEGASUS_TEST_ASSERT(b1 == a1);
    PEGASUS_TEST_ASSERT(b1 == c1);
    PEGASUS_TEST_ASSERT(c1 == a1);
    PEGASUS_TEST_ASSERT(c1 == b1);

    PEGASUS_TEST_ASSERT(a2 != b1);
    PEGASUS_TEST_ASSERT(a2 != c1);
    PEGASUS_TEST_ASSERT(b2 != a1);
    PEGASUS_TEST_ASSERT(b2 != c1);
    PEGASUS_TEST_ASSERT(c2 != a1);
    PEGASUS_TEST_ASSERT(c2 != b1);

    PEGASUS_TEST_ASSERT(a2 >= b1);
    PEGASUS_TEST_ASSERT(a2 >= c1);
    PEGASUS_TEST_ASSERT(b2 >= a1);
    PEGASUS_TEST_ASSERT(b2 >= c1);
    PEGASUS_TEST_ASSERT(c2 >= a1);
    PEGASUS_TEST_ASSERT(c2 >= b1);

    PEGASUS_TEST_ASSERT(a2 <= b3);
    PEGASUS_TEST_ASSERT(a2 <= c3);
    PEGASUS_TEST_ASSERT(b2 <= a3);
    PEGASUS_TEST_ASSERT(b2 <= c3);
    PEGASUS_TEST_ASSERT(c2 <= a3);
    PEGASUS_TEST_ASSERT(c2 <= b3);

    PEGASUS_TEST_ASSERT(a2 > b1);
    PEGASUS_TEST_ASSERT(a2 > c1);
    PEGASUS_TEST_ASSERT(b2 > a1);
    PEGASUS_TEST_ASSERT(b2 > c1);
    PEGASUS_TEST_ASSERT(c2 > a1);
    PEGASUS_TEST_ASSERT(c2 > b1);

    PEGASUS_TEST_ASSERT(a2 < b3);
    PEGASUS_TEST_ASSERT(a2 < c3);
    PEGASUS_TEST_ASSERT(b2 < a3);
    PEGASUS_TEST_ASSERT(b2 < c3);
    PEGASUS_TEST_ASSERT(c2 < a3);
    PEGASUS_TEST_ASSERT(c2 < b3);

    //Overflow testing
    CQLValue real1(Real64(0.00000001));
    CQLValue sint1(Sint64(-1));
    CQLValue uint1(Sint64(1));
    CQLValue uint2(Uint64(0));

    PEGASUS_TEST_ASSERT(uint1 > sint1);
    PEGASUS_TEST_ASSERT(real1 > sint1);
    PEGASUS_TEST_ASSERT(uint2 > sint1);
    PEGASUS_TEST_ASSERT(real1 > uint2);

    CQLValue real2(Real64(25.00000000000001));
    CQLValue real3(Real64(24.99999999999999));
    CQLValue sint2(Sint64(25));
    CQLValue uint3(Uint64(25));

    PEGASUS_TEST_ASSERT(real2 > real3);
    PEGASUS_TEST_ASSERT(real2 > sint2);
    PEGASUS_TEST_ASSERT(real2 > uint3);
    PEGASUS_TEST_ASSERT(real3 < sint2);
    PEGASUS_TEST_ASSERT(real3 < uint3);

    // String tests

    CQLValue d1(String("HELLO"));
    CQLValue d2(String("HEL"));
    CQLValue d3(String("LO"));
    CQLValue d4(String("AHELLO"));
    CQLValue d5(String("ZHELLO"));

    PEGASUS_TEST_ASSERT(d1 == d2 + d3);
    PEGASUS_TEST_ASSERT(d1 != d2 + d4);

    PEGASUS_TEST_ASSERT(d1 <= d5);
    PEGASUS_TEST_ASSERT(d1 <  d5);

    PEGASUS_TEST_ASSERT(d1 >= d4);
    PEGASUS_TEST_ASSERT(d1 >  d4);

    String str1("0x10");
    String str2("10");
    String str3("10B");
    String str4("10.10");


    CQLValue e1( str1, CQLValue::Hex);
    CQLValue e2( str2, CQLValue::Decimal);
    CQLValue e3( str3, CQLValue::Binary);
    CQLValue e4( str4, CQLValue::Real);

    CQLValue e5(Uint64(16));
    CQLValue e6(Uint64(10));
    CQLValue e7(Uint64(2));
    CQLValue e8(Real64(10.10));

    PEGASUS_TEST_ASSERT(e1 == e5);
    PEGASUS_TEST_ASSERT(e2 == e6);
    PEGASUS_TEST_ASSERT(e3 == e7);
    PEGASUS_TEST_ASSERT(e4 == e8);

    Array<Uint64> array1;

    array1.append(1);
    array1.append(2);
    array1.append(3);
    array1.append(4);
    array1.append(5);
    array1.append(6);
    array1.append(7);
    array1.append(8);
    array1.append(9);
    array1.append(10);

    Array<Sint64> array2;

    array2.append(1);
    array2.append(2);
    array2.append(3);
    array2.append(4);
    array2.append(5);
    array2.append(6);
    array2.append(7);
    array2.append(8);
    array2.append(9);
    array2.append(10);
    array2.append(3);

    Array<Real64> array3;

    array3.append(1.00);
    array3.append(2.00);
    array3.append(3.00);
    array3.append(9.00);
    array3.append(10.00);
    array3.append(3.00);
    array3.append(4.00);
    array3.append(5.00);
    array3.append(6.00);
    array3.append(7.00);
    array3.append(8.00);

    Array<Uint64> array4;

    array4.append(1);
    array4.append(23);
    array4.append(3);
    array4.append(4);
    array4.append(5);
    array4.append(6);
    array4.append(7);
    array4.append(88);
    array4.append(9);
    array4.append(10);

    Array<Sint64> array5;

    array5.append(-1);
    array5.append(2);
    array5.append(3);
    array5.append(4);
    array5.append(5);
    array5.append(-6);
    array5.append(7);
    array5.append(8);
    array5.append(9);
    array5.append(10);
    array5.append(-3);

    Array<Real64> array6;

    array6.append(1.23);
    array6.append(2.00);
    array6.append(3.00);
    array6.append(9.00);
    array6.append(10.00);
    array6.append(3.00);
    array6.append(4.14);
    array6.append(5.00);
    array6.append(6.00);
    array6.append(7.00);
    array6.append(8.00);

    CIMValue cv1(array1);
    CIMValue cv2(array2);
    CIMValue cv3(array3);
    CIMValue cv4(array4);
    CIMValue cv5(array5);
    CIMValue cv6(array6);

    CQLValue vr1(cv1);
    CQLValue vr2(cv1);
    CQLValue vr3(cv2);
    CQLValue vr4(cv3);
    CQLValue vr5(cv4);
    CQLValue vr6(cv5);
    CQLValue vr7(cv6);

    PEGASUS_TEST_ASSERT(vr1 == vr2);
    PEGASUS_TEST_ASSERT(vr1 == vr3);
    PEGASUS_TEST_ASSERT(vr1 == vr4);
    PEGASUS_TEST_ASSERT(vr4 == vr3);

    PEGASUS_TEST_ASSERT(vr1 != vr5);
    PEGASUS_TEST_ASSERT(vr3 != vr6);
    PEGASUS_TEST_ASSERT(vr4 != vr7);

    const CIMName _cimName(String("CIM_OperatingSystem"));

    CIMInstance _i1(_cimName);
    CIMProperty _p1(CIMName("Description"),CIMValue(String("Dave Rules")));
    CIMProperty _p2(CIMName("EnabledState"),CIMValue(Uint16(2)));
    CIMProperty _p3(CIMName("CurrentTimeZone"),CIMValue(Sint16(-600)));
    CIMProperty _p4(CIMName("TimeOfLastStateChange"),
                    CIMValue(CIMDateTime(String("20040811105625.000000-360"))));

    _i1.addProperty(_p1);
    _i1.addProperty(_p2);
    _i1.addProperty(_p3);
    _i1.addProperty(_p4);

    CIMInstance _i2(_cimName);
    CIMProperty _p5(CIMName("Description"),
                    CIMValue(String("Dave Rules Everything")));
    CIMProperty _p6(CIMName("EnabledState"),CIMValue(Uint16(2)));
    CIMProperty _p7(CIMName("CurrentTimeZone"),CIMValue(Sint16(-600)));
    CIMProperty _p8(CIMName("TimeOfLastStateChange"),
                    CIMValue(CIMDateTime(String("20040811105625.000000-360"))));

    _i2.addProperty(_p5);
    _i2.addProperty(_p6);
    _i2.addProperty(_p7);
    _i2.addProperty(_p8);

    CQLValue cql1(_i1);
    CQLValue cql2(_i1);
    CQLValue cql3(_i2);
    CQLValue cql4(_i2);

    //PEGASUS_TEST_ASSERT(cql1 == cql1);

    return;
}
Example #12
0
bool VectorTest::performTest()
{
    srand((unsigned)time(NULL));

    const Elem cv_data[] = {  1, 2, 3 };
    const Elem rv_data[] = { -4, 0, 8 };
    ColVector cv(3, cv_data);
    RowVector rv(3, rv_data);
    cout << "Vector cv: " << cv << endl
         << "Vector rv: " << rv << endl;

    // Shift
    {
        const Elem cv_left[] = { 2, 3, 0 };
        const Elem cv_right[] = { 0, 1, 2 };
        ColVector cvl(cv);
        cvl.shiftLeft();
        cout << "Vector rv shifted left: " << cvl << endl;
        if (ColVector(3, cv_left) != cvl) return false;
        ColVector cvr(cv);
        cvr.shiftRight();
        cout << "Vector rv shifted right: " << cvr << endl;
        if (ColVector(3, cv_right) != cvr) return false;
    }


    // rv + rv
    {
        RowVector result = rv + rv;
        cout << "rv + rv = " << result << endl;
        for (unsigned int i = 0; i < rv.dim(); i++) {
            if (!epsilonCheck(result(i), rv(i) + rv(i)))
                return false;
        }
    }

    // rv - rv
    {
        RowVector result = rv - rv;
        cout << "rv - rv = " << result << endl;
        for (unsigned int i = 0; i < rv.dim(); i++) {
            if (!epsilonCheck(result(i), rv(i) - rv(i)))
                return false;
        }
    }

    // cv + cv
    {
        ColVector result = cv + cv;
        cout << "cv + cv = " << result << endl;
        for (unsigned int i = 0; i < cv.dim(); i++) {
            if (!epsilonCheck(result(i), cv(i) + cv(i)))
                return false;
        }
    }

    // cv - cv
    {
        ColVector result = cv - cv;
        cout << "cv - cv = " << result << endl;
        for (unsigned int i = 0; i < cv.dim(); i++) {
            if (!epsilonCheck(result(i), cv(i) - cv(i)))
                return false;
        }
    }

    // Operators += and -=
    {
        ColVector cvc(cv);
        cout << "cvc = " << cvc << endl;

        cvc += cv;
        cout << "cvc += cv => " << cvc << endl;
        if (!epsilonCheck(cvc(0), 2) ||
            !epsilonCheck(cvc(1), 4) ||
            !epsilonCheck(cvc(2), 6))
            return false;

        cvc -= cv;
        cout << "cvc -= cv => " << cvc << endl;
        if (!epsilonCheck(cvc(0), 1) ||
            !epsilonCheck(cvc(1), 2) ||
            !epsilonCheck(cvc(2), 3))
            return false;

        RowVector rvc(rv); // -4 0 8
        cout << "rvc = " << rvc << endl;

        rvc += rv;
        cout << "rvc += rv => " << rvc << endl;
        if (!epsilonCheck(rvc(0), -8) ||
            !epsilonCheck(rvc(1), 0) ||
            !epsilonCheck(rvc(2), 16))
            return false;

        rvc -= rv;
        cout << "rvc -= rv => " << rvc << endl;
        if (!epsilonCheck(rvc(0), -4) ||
            !epsilonCheck(rvc(1), 0) ||
            !epsilonCheck(rvc(2), 8))
            return false;
    }

    // Operator *=
    {
        ColVector cvc(cv);
        cout << "cvc = " << cvc << endl;
        cvc *= 5;
        cout << "cvc *= 5 => " << cvc << endl;
        if (!epsilonCheck(cvc(0), 5) ||
            !epsilonCheck(cvc(1), 10) ||
            !epsilonCheck(cvc(2), 15))
            return false;
    }

    // rv * cv
    {
        const Elem inner_prod = rv * cv;
        cout << "rv * cv = " << inner_prod << endl;
        if (!epsilonCheck(inner_prod, 20))
            return false;
    }

    // cv * rv
    {
        const Matrix cvrv(cv * rv);
        cout << "cv * rv = " << endl << cvrv;
        const Elem correct_cvrv_data[] = {  -4, 0,  8,
                                              -8, 0, 16,
                                             -12, 0, 24 };
        const Matrix correct_cvrv(3, 3, correct_cvrv_data);
        if (correct_cvrv != cvrv)
            return false;
    }

    // cv * rv larger test
    {
        int n_tests = 100;
        for (int k = 0; k < n_tests; ++k) {
            cout << "cv * rv #" << k << endl;
            const int m = 25;
            const int n = 500;
            double* data1 = new double[m];
            double* data2 = new double[n];
            for (int i = 0; i < m; ++i)
                data1[i] = generators::random(i);
            for (int i = 0; i < n; ++i)
                data2[i] = generators::random(i);
            ColVector randomCv(m, data1);
            RowVector randomRv(n, data2);
            Matrix cvAsMatrix(m, 1, data1);
            Matrix rvAsMatrix(1, n, data2);
            Matrix ref(m, n);
            cvAsMatrix.multWithMatrix(rvAsMatrix, &ref);
            Matrix res(randomCv * randomRv);
            if (ref != res) 
                return false;
            delete[] data2;
            delete[] data1;
        }
    }

    // cv * 5
    {
        ColVector tmp = cv * 5;
        cout << "cv * 5 = " << tmp << endl;
        if (!epsilonCheck(tmp(0), 5) ||
            !epsilonCheck(tmp(1), 10) ||
            !epsilonCheck(tmp(2), 15))
            return false;
    }

    // 4 * cv
    {
        ColVector tmp = 4 * cv;
        cout << "4 * cv = " << tmp << endl;
        if (!epsilonCheck(tmp(0), 4) ||
            !epsilonCheck(tmp(1), 8) ||
            !epsilonCheck(tmp(2), 12))
            return false;
    }

    // rv * 5
    {
        RowVector tmp = rv * 5;
        cout << "rv * 5 = " << tmp << endl;
        if (!epsilonCheck(tmp(0), -20) ||
            !epsilonCheck(tmp(1), 0) ||
            !epsilonCheck(tmp(2), 40))
            return false;
    }

    // 4 * rv
    {
        RowVector tmp = 4 * rv;
        cout << "4 * rv = " << tmp << endl;
        if (!epsilonCheck(tmp(0), -16) ||
            !epsilonCheck(tmp(1), 0) ||
            !epsilonCheck(tmp(2), 32))
            return false;
    }

    // Maximum, minimum
    const Elem mv_data[] = { -16, -1, 4, 8 };
    RowVector mv(4, mv_data);
    cout << "mv = " << mv << endl;

    Elem m = mv.minimum();
    cout << "Minimum of mv:            " << m << endl;
    if (m != -16)
        return false;
    
    m = mv.minimum(true);
    cout << "Minimum of mv (absolute): " << m << endl;
    if (m != -1)
        return false;

    m = mv.maximum();
    cout << "Maximum of mv:            " << m << endl;
    if (m != 8)
        return false;
    
    m = mv.maximum(true);
    cout << "Maximum of mv (absolute): " << m << endl;
    if (m != -16)
        return false;

    // Length
    cout << "Length of rv: " << rv.length() << endl;
    if (!epsilonCheck(rv.length(), 8.94427191))
        return false;
    
    // Angle between cv and rv
    cout << "Angle between cv and rv: " << Vector::angle(cv, rv) << endl;
    if (!epsilonCheck(Vector::angle(cv, rv), 0.930274014115))
        return false;
    
    // Random vector of unit length
    ColVector randcv(3, generators::random);
    cout << "Random vector: " << randcv << endl;

    // Silent operator() check
    // In case of errors this would fail during compile time
    {
    	ColVector a(5);
    	const ColVector b(3);
    	a(1) = b(2);
    }
    
    // Dump and read
    {
        const Elem v_data[] = {   3, 2.5, 1.5,   -1, -2.5, -1.5 };
        RowVector rv(6, v_data);
        ColVector cv(6, v_data);
        Poco::TemporaryFile rvTmpFile, cvTmpFile;
        rv.dump(rvTmpFile.path());
        cv.dump(cvTmpFile.path());
        RowVector rv2(rvTmpFile.path());
        ColVector cv2(cvTmpFile.path());
        cout << "---" << endl
             << "rv = " << rv << endl
             << "cv = " << cv << endl
             << "rv from file: " << rv2 << endl
             << "cv from file: " << cv2 << endl;
        for (unsigned int i = 0; i < 6; ++i)
            if (rv(i) != rv2(i) || cv(i) != cv2(i))
                return false;
    }
    
    return true;
}
Example #13
0
void TestBandDiv(tmv::DivType dt)
{
    const int N = 10;

    std::vector<tmv::BandMatrixView<T> > b;
    std::vector<tmv::BandMatrixView<std::complex<T> > > cb;
    MakeBandList(b,cb);

    tmv::Matrix<T> a1(N,N);
    for (int i=0; i<N; ++i) for (int j=0; j<N; ++j) a1(i,j) = T(3+i-2*j);
    a1.diag().addToAll(T(10)*N);
    a1 /= T(10);

    tmv::Matrix<std::complex<T> > ca1 = a1 * std::complex<T>(3,-4);

    tmv::Vector<T> v1(N);
    tmv::Vector<T> v2(N-1);
    for (int i=0; i<N; ++i) v1(i) = T(16-3*i);
    for (int i=0; i<N-1; ++i) v2(i) = T(-6+i); 
    tmv::Vector<std::complex<T> > cv1(N);
    tmv::Vector<std::complex<T> > cv2(N-1);
    for (int i=0; i<N; ++i) cv1(i) = std::complex<T>(16-3*i,i+4); 
    for (int i=0; i<N-1; ++i) cv2(i) = std::complex<T>(2*i-3,-6+i); 

    tmv::Matrix<T> a3 = a1.colRange(0,N/2);
    tmv::Matrix<std::complex<T> > ca3 = ca1.colRange(0,N/2);
    tmv::Matrix<T> a4 = a1.rowRange(0,N/2);
    tmv::Matrix<std::complex<T> > ca4 = ca1.rowRange(0,N/2);
    tmv::Matrix<T> a5 = a1.colRange(0,0);
    tmv::Matrix<std::complex<T> > ca5 = ca1.colRange(0,0);
    tmv::Matrix<T> a6 = a1.rowRange(0,0);
    tmv::Matrix<std::complex<T> > ca6 = ca1.rowRange(0,0);
    tmv::Matrix<T> a7 = a1;
    tmv::Matrix<std::complex<T> > ca7 = ca1;
    a7.diag().addToAll(T(10)*N);
    ca7.diag().addToAll(T(10)*N);

    for(size_t i=START;i<b.size();i++) {
        if (showstartdone) 
            std::cout<<"Start loop: i = "<<i<<"\nbi = "<<tmv::TMV_Text(b[i])<<
                "  "<<b[i]<<std::endl;
        tmv::BandMatrixView<T> bi = b[i];
        tmv::BandMatrixView<std::complex<T> > cbi = cb[i];
        if (dt == tmv::LU && !bi.isSquare()) continue;

        bi.saveDiv();
        cbi.saveDiv();

        tmv::Matrix<T> m(bi);
        m.saveDiv();
        bi.divideUsing(dt);
        bi.setDiv();
        m.divideUsing(dt);
        m.setDiv();

        std::ostream* divout = showdiv ? &std::cout : 0;
        Assert(bi.checkDecomp(divout),"CheckDecomp");
        T eps = m.rowsize()*EPS*Norm(m)*Norm(m.inverse());

        if (bi.colsize() == N) {
            tmv::Vector<T> x1 = v1/bi;
            tmv::Vector<T> x2 = v1/m;
            if (showacc) {
                std::cout<<"v/b: Norm(x1-x2) = "<<Norm(x1-x2)<<
                    "  "<<eps*Norm(x1)<<std::endl;
            }
            Assert(Norm(x1-x2) < eps*Norm(x1),"Band v/b");
        }

        if (bi.rowsize() == N) {
            tmv::Vector<T> x1 = v1%bi;
            tmv::Vector<T> x2 = v1%m;
            if (showacc) {
                std::cout<<"v%b: Norm(x1-x2) = "<<Norm(x1-x2)<<
                    "  "<<eps*Norm(x1)<<std::endl;
            }
            Assert(Norm(x1-x2) < eps*Norm(x1),"Band v%b");
        }

        tmv::Matrix<T,tmv::ColMajor> binv = bi.inverse();
        tmv::Matrix<T,tmv::ColMajor> minv = m.inverse();
        if (showacc) {
            std::cout<<"minv = "<<minv<<std::endl;
            std::cout<<"binv = "<<binv<<std::endl;
            std::cout<<"Norm(minv-binv) = "<<Norm(minv-binv)<<
                "  "<<eps*Norm(binv)<<std::endl;
        }
        Assert(Norm(binv-minv) < eps*Norm(binv),"Band Inverse");

        if (m.isSquare()) {
            if (showacc) {
                std::cout<<"Det(b) = "<<Det(bi)<<
                    ", Det(m) = "<<Det(m)<<std::endl;
                std::cout<<"abs(bdet-mdet) = "<<std::abs(Det(bi)-Det(m));
                std::cout<<"  EPS*abs(mdet) = "<<
                    eps*std::abs(Det(m))<<std::endl;
                std::cout<<"abs(abs(bdet)-abs(mdet)) = "<<
                    std::abs(std::abs(Det(bi))-std::abs(Det(m)));
                std::cout<<"  EPS*abs(mdet) = "<<
                    eps*std::abs(Det(m))<<std::endl;
            }
            Assert(std::abs(Det(m)-Det(bi)) < eps*std::abs(Det(m)+Norm(m)),
                   "Band Det");
            T msign, bsign;
            Assert(std::abs(m.logDet(&msign)-bi.logDet(&bsign)) < N*eps,
                   "Band LogDet");
            Assert(std::abs(msign-bsign) < N*eps,"Band LogDet - sign");
        }

        cbi.divideUsing(dt);
        cbi.setDiv();
        Assert(cbi.checkDecomp(divout),"CheckDecomp");

        tmv::Matrix<std::complex<T> > cm(cbi);
        cm.saveDiv();
        cm.divideUsing(dt);
        cm.setDiv();
        T ceps = EPS*Norm(cm)*Norm(cm.inverse());

        if (cm.isSquare()) {
            if (showacc) {
                std::cout<<"Det(cbi) = "<<Det(cbi)<<", Det(cm) = "<<
                    Det(cm)<<std::endl;
                std::cout<<"abs(cbidet-cmdet) = "<<std::abs(Det(cbi)-Det(cm));
                std::cout<<"  cbidet/cmdet = "<<Det(cbi)/Det(cm);
                std::cout<<"  EPS*abs(cmdet) = "<<
                    ceps*std::abs(Det(cm))<<std::endl;
                std::cout<<"abs(abs(bdet)-abs(mdet)) = "<<
                    std::abs(std::abs(Det(bi))-std::abs(Det(m)));
                std::cout<<"  EPS*abs(mdet) = "<<
                    ceps*std::abs(Det(m))<<std::endl;
            }
            Assert(std::abs(Det(cbi)-Det(cm)) < 
                   ceps*std::abs(Det(cm)+Norm(cm)),
                   "Band CDet");
            std::complex<T> cmsign, cbsign;
            Assert(std::abs(cm.logDet(&cmsign)-cbi.logDet(&cbsign)) < N*eps,
                   "Band CLogDet");
            Assert(std::abs(cmsign-cbsign) < N*eps,"Band CLogDet - sign");
        }

        tmv::Vector<std::complex<T> > cv(v1 * std::complex<T>(1,1));
        cv(1) += std::complex<T>(-1,5);
        cv(2) -= std::complex<T>(-1,5);

        if (m.colsize() == N) {
            // test real / complex
            tmv::Vector<std::complex<T> > y1 = v1/cbi;
            tmv::Vector<std::complex<T> > y2 = v1/cm;
            if (showacc) {
                std::cout<<"v/cb: Norm(y1-y2) = "<<Norm(y1-y2)<<
                    "  "<<ceps*Norm(y1)<<std::endl;
            }
            Assert(Norm(y1-y2) < ceps*Norm(y1),"Band v/cb");

            // test complex / real
            y1 = cv/bi;
            y2 = cv/m;
            if (showacc) {
                std::cout<<"cv/b: Norm(y1-y2) = "<<Norm(y1-y2)<<
                    "  "<<eps*Norm(y1)<<std::endl;
            }
            Assert(Norm(y1-y2) < eps*Norm(y1),"Band cv/b");

            // test complex / complex
            y1 = cv/cbi;
            y2 = cv/cm;
            if (showacc) {
                std::cout<<"cv/cb: Norm(y1-y2) = "<<Norm(y1-y2)<<
                    "  "<<ceps*Norm(y1)<<std::endl;
            }
            Assert(Norm(y1-y2) < ceps*Norm(y1),"Band cv/cb");
        }

        if (bi.rowsize() == N) {
            tmv::Vector<std::complex<T> > y1 = v1%cbi;
            tmv::Vector<std::complex<T> > y2 = v1%cm;
            if (showacc) {
                std::cout<<"v%cb: Norm(y1-y2) = "<<Norm(y1-y2)<<
                    "  "<<ceps*Norm(y1)<<std::endl;
            }
            Assert(Norm(y1-y2) < ceps*Norm(y1),"Band v%cb");

            y1 = cv%bi;
            y2 = cv%m;
            if (showacc) {
                std::cout<<"cv%b: Norm(y1-y2) = "<<Norm(y1-y2)<<
                    "  "<<eps*Norm(y1)<<std::endl;
            }
            Assert(Norm(y1-y2) < eps*Norm(y1),"Band cv%b");
            y1 = cv%cbi;
            y2 = cv%cm;
            if (showacc) {
                std::cout<<"cv%cb: Norm(y1-y2) = "<<Norm(y1-y2)<<
                    "  "<<ceps*Norm(y1)<<std::endl;
            }
            Assert(Norm(y1-y2) < ceps*Norm(y1),"Band cv%cb");
        }

    }

    TestBandDiv_A<T>(dt);
    TestBandDiv_B1<T>(dt);
    TestBandDiv_B2<T>(dt);
    TestBandDiv_C1<T>(dt);
    if (dt == tmv::LU) TestBandDiv_C2<T>(dt);
    TestBandDiv_D1<T>(dt);
    if (dt == tmv::LU) TestBandDiv_D2<T>(dt);

    std::cout<<"BandMatrix<"<<tmv::TMV_Text(T())<<"> Division using ";
    std::cout<<tmv::TMV_Text(dt)<<" passed all tests\n";
}