Blitz logo

Blitz Support :

From: Julian Cummings (cummings_at_[hidden])
Date: 2003-11-19 19:08:23


Hello Olivier,

> The following code does not work, I can not understand why
>
> #include <blitz/array.h>
> #include <blitz/vector.h>
> #include <blitz/tinymat.h>
> #include <iostream>
> #include <complex>
>
> BZ_USING_NAMESPACE(blitz)
> using namespace std;
>
> typedef Vector< TinyMatrix< complex<double>, 5,5> > densityMatrix;
>
> int main(void) {
> densityMatrix rho(3);
> rho(0)=0;
> (rho(0))(1,1)=1515;
> rho(1)=1;
> rho(2)=2;
>
> cout << rho(1)-rho(2) << endl; // This line does not compile
> }
>
> The error is
> test.cpp: In function `int main()':
> test.cpp:39: error: no match for 'operator-' in '
> (&rho)->blitz::Vector<P_numtype>::operator()(int) [with
> P_numtype =
> blitz::TinyMatrix<std::complex<double>, 5, 5>](1) -
> (&rho)->blitz::Vector<P_numtype>::operator()(int) [with
> P_numtype =
> blitz::TinyMatrix<std::complex<double>, 5, 5>](2)'
> /usr/local/include/blitz/vecexpr.h:262: error: candidates are:
> blitz::_bz_VecExpr<blitz::_bz_VecExprUnaryOp<blitz::Range,
> blitz::_bz_negate<int> > > blitz::operator-(blitz::Range)

Unfortunately, the TinyMatrix class in blitz is incomplete and the typical
math operations are not provided. I am in the process of bringing the tvmet
classes in to replace the blitz Tiny classes. The tvmet package grew from
the original blitz Tiny classes and now has complete expression template
support. Rather than replicate the work of tvmet, I am trying to simply
fold it back into blitz. I hope to have this working before the end of this
year. In the meantime, you can work around this by defining whatever simple
function you may need on your own. For example,

namespace blitz {
template <typename T_numtype, int N_rows, int N_cols>
TinyMatrix<T_numtype,N_rows,N_cols>
operator-(TinyMatrix<T_numtype,N_rows,N_cols> a,
TinyMatrix<T_numtype,N_rows,N_cols>& b)
{
    TinyMatrix<T_numtype,N_rows,N_cols> c;
    for (int i = 0; i < N_rows; i++)
        for (int j = 0; j < N_cols; j++)
            c(i,j) = a(i,j) - b(i,j);
    return c;
}
}

>
> In my another method, I try to compute the trace of a complex
> matrix with std::complex<double> trace(const
> blitz::Array<std::complex<double>, 2>& A){
> firstIndex i;
> return sum(A(i,i), i);
> }
>
> blas.cpp: In function `std::complex<double> trace(const
> blitz::Array<std::complex<double>, 2>&)':
> blas.cpp:38: error: conversion from `
>
> blitz::_bz_ArrayExpr<blitz::_bz_ArrayExprReduce<blitz::_bz_Arr
> ayExpr<blitz::ArrayIndexMapping<std::complex<double>,
> 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0> >, 0,
> blitz::ReduceSum<std::complex<double>,
> std::complex<double> > > >' to
> non-scalar type `std::complex<double>' requested
>
> Is it related to sum or is it just a C++ thing?

This is just a misunderstanding of how partial reductions work in blitz.
You can reduce along a particular dimension of an Array using this notation,
but it doesn't handle a reduction along the diagonal as required for the
trace function. Try using the where function of blitz to sum only the
diagonal elements:

std::complex<double> trace(const blitz::Array<std::complex<double>,2>& A) {
    std::complex<double> zero(0.0,0.0);
    blitz::firstIndex i;
    blitz::secondIndex j;
    return sum(where(i==j,A(i,j),zero));
}

>
> Thanks a lot!
>
> PS I use the FreeBSD port of Blitz 0.7
> (http://www.freebsd.org/cgi/cvsweb.cgi/ports/math/blitz%2b%2b/) which
> uses the following dirty hack
>
> --- doc/Makefile.in.orig Sat Nov 8 00:45:38 2003
> +++ doc/Makefile.in Sat Nov 8 00:45:33 2003
> @@ -155,7 +155,7 @@ SUBDIRS = examples stencils
>
> # install these files
> docdir = $(prefix)/doc
> -doc_DATA = blitz.ps
> +doc_DATA =
>
> AM_MAKEINFOFLAGS = --no-split
> TEXI2HTML = texi2html
>
>
> to avoid installing blitz.ps in $prefix/doc, whereas the other files
> from Blitz++-0.7/doc are installed in $prefix/doc/blitz. Is
> it possible
> to install this file directly into $prefix/doc/blitz?

OK, I'll check in a fix for this into the repository at some point.
Thanks for pointing this out.

Regards, Julian C.