Example: Initialization

/*
 * Expression Template Matrix Library
 *
 * Copyright (C) 2004 - 2004 Ricky Lung <mtlung@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 *  Demonstrate different methods of initialization
 */

#include <iostream>
#include "../include/exmat.h"

using namespace std;
using namespace exmat;

// Define some vector and matrix types

// A row vector of dynamic size, with float as the element type
typedef Vec<DenseRowVecCon<float, 0> > Vecf;
// A row vector of static size 3, with float as the element type
typedef Vec<DenseRowVecCon<float, 3> > Vec3f;
// Dynamic size matrix, with float as the element type
typedef Mat<DenseMatCon<float,0,0> > Matf;
// A dense matrix of static size 3x3, with float as the element type
typedef Mat<DenseMatCon<float,3,3> > Mat3x3f;

int main()
{
    // Direct initialization

    // Initialize all the elements with scaler value 0.1
    Vec3f v1(0.1f);
    cout << v1 << endl << endl;

    // Initialize with a string
    Vec3f v2("0 1 2");
    cout << v2 << endl << endl;

    // Initialization from another vector
    // (note that the size of the dynamic vector is also initialized)
    Vecf v3(v2);
    cout << v3 << endl << endl;

    // Initialize from an expression
    Vecf v4(v1 + v2);
    cout << v4 << endl << endl;

    // Assignment initialization

    // Initialize all the element with scaler value 1.1
    Vec3f v5 = 1.1f;
    cout << v5 << endl << endl;

    // Initialization from another vector
    Vec3f v6 = v5;
    cout << v6 << endl << endl;

    // Initialize from an expression
    Vecf v7 = v5 + v4;
    cout << v7 << endl << endl;

    // Initializing dynamic size
    int a = 3;
    Dim dim(a);
    Vecf v8(dim, 1.0f);
    cout << v8 << endl << endl;

    // Initialize dynamic vector from a string,
    // the first element in the string will be used as the size
    Vecf v9("5 0 1 2 3 4 5");
    cout << v9 << endl << endl;

    // Comma initialization
    Mat3x3f m1;
    m1 = 1, 2, 3,
         4, 5, 6,
         7, 8, 9;
    cout << m1 << endl << endl;
    // Note: comma initialization will not resize dynamic matrix

    // Note: you cannot do this:
/*
    Mat3x3f m1 =
        1, 2, 3,
        4, 5, 6,
        7, 8, 9;
*/

    // Initialize dynamic matrix from a string,
    // the first element in the string will be used as the no. of rows,
    // second element as no. of columns
    Matf m2("   3 4 \
                1 4 7 10\n\
                2 5 8 11\n\
                3 6 9 12\n\
                ");
    cout << m2;

    return 0;
}

Generated on Sat May 6 23:12:04 2006 for Exmat by  doxygen 1.4.6-NO