File size: 2,662 Bytes
68ce2fc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
/*
Author(s): Anton Deguet, Ofri Sadowsky
Created on: 2003-10-15
(C) Copyright 2003-2007 Johns Hopkins University (JHU), All Rights
Reserved.
--- begin cisst license - do not edit ---
This software is provided "as is" under an open source license, with
no warranty. The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.
--- end cisst license ---
*/
// [doc-example-start]
void ExampleDynamic(void) {
// define our prefered types
typedef vctDynamicVector<double> VectorType;
typedef vctDynamicMatrix<double> MatrixType;
// The dynamic vector library may throw exceptions,
// (derived from std::exception)
// so we place the operations in a try-catch block.
try {
// create an empty vector
VectorType vector1;
std::cout << "Size of vector1: " << vector1.size() << std::endl;
// resize and fill the vector
unsigned int index;
vector1.SetSize(5);
for (index = 0; index < vector1.size(); index++) {
vector1[index] = index;
}
// look at product of elements
std::cout << "Product of elements is 0? "
<< vector1.ProductOfElements() << std::endl;
// create a matrix initialized with zeros
MatrixType matrix1(7, vector1.size());
matrix1.SetAll(0.0);
// set the diagonal to 5.0
matrix1.Diagonal().SetAll(5.0);
// look at the sum/product of elements
std::cout << "Sum of elements is 25? "
<< matrix1.SumOfElements() << std::endl;
// multiply matrix1 by vector 2
VectorType vector2(matrix1.rows());
vector2.ProductOf(matrix1, vector1);
// multiply vector1 directly
VectorType vector3(vector1.size());
vector3.ProductOf(5.0, vector1);
// resize vector2 while preserving the data
vector2.resize(vector3.size());
// vector2 and vector3 are the same?
VectorType difference;
difference = vector3 - vector2;
difference.AbsSelf();
std::cout << "Maximum difference between v2 and v3: "
<< difference.MaxElement() << std::endl;
// alternative solution
std::cout << "Maximum difference between v2 and v3: "
<< (vector3 - vector2).MaxAbsElement() << std::endl;
} // end of try block
// catch block
catch (std::exception & Exception) {
std::cerr << "Exception occured: " << Exception.what() << std::endl;
}
}
// [doc-example-end]
|