blitzpp / blitz

Blitz++ Multi-Dimensional Array Library for C++
https://github.com/blitzpp/blitz/wiki
Other
405 stars 84 forks source link

User defined member type (*array).a() #84

Open slayoo opened 5 years ago

slayoo commented 5 years ago

Migrated from SF: https://sourceforge.net/p/blitz/patches/3/ Issue reported by @mdsuresh back in 2002:

I read the blitz documentation on user defined types.

For example let's take a

struct Foo {
enum { value_a =0 , value_b = 1...}
int a;
int b;
int c;
}

Its seems in blitz currently that if a user decided to access a single member of the entire array of Foo, the user has to resort to something like

Array<Foo, 2> test;
Array<int,2> test_a = test[ Foo::value_a]

while a better C++ style of member return would be

Array<int,2> test_a= (*test).a();

Well, i think the following suggestion may help over the current problem of test[ Foo::value_a] and sucessful replacement by (*test).a()

Well the better thing to shoot for would be

test->a()

however, it need creation of a pointer object that needs to be automatically disposed. may be adding "delete *this;" to a() function would help :-/

I have also included func.cpp with this post so that u can test my ideas and see that this code will compile.

Just replace the old func.cpp file with this one. And if you run into problems, just contact me. :-)

Cheers -Suresh

// Suresh kumar Devenathan
// mdsuresh@yahoo.com
// (C) GNU Public License
// compilation test program for the blitz library

#define BZ_DEBUG
#include<iostream>
#include<algorithm>

#include<blitz/array.h>
#include <blitz/array/indirect.h>
#include<vector>

#ifdef BZ_NAMESPACES
using namespace blitz;
using namespace std;
#endif

#include <functions/inverse.h>
#include <functions/fourier.h>
#include <functions/walsh.h>
#include <functions/transform.h>
#include <functions/filter.h>
#include <functions/svd.h>
#include <functions/lu.h>

template<class T, int n>
struct Exporter {
public:
    struct T_Export
    {
      T_Export(Array<T,n> _m) {
      }

    };

};

#define BEGIN_EXPORT_BLOCK(structure, component_type)  template<int n>  \
     struct Exporter< structure , n > {  \
     public: \
       struct  T_Export  {  \
        typedef structure T_structure;   \
        typedef component_type T_component;\
        Array<T_structure, n> & main;  \
        T_structure t; \
        T_Export(Array<T_structure,  n>& _input) : main(_input) {  } 

#define EXPORT_MEMBER(member)    \
          Array<T_component, n>  member() { \
           int move = 0;  \
           while(  (((T_component *) & t. ## member ) - ( ((T_component*) &t) + move) ) !=0) move++; \
           return  main.extractComponent( T_component(),  \
             move ,    sizeof(T_structure)/ sizeof(T_component) );      } 

#define END_EXPORT_BLOCK }; \
       };    

struct Sample {
    int a;
    int b;
    int c;
};

BEGIN_EXPORT_BLOCK(Sample, int)
        EXPORT_MEMBER(a)
END_EXPORT_BLOCK

template<class T, int rank>
struct Test {
   typedef Exporter<T,rank> T_Exportor;
   typedef T_Exportor::T_Export T_Export;

   T_Export operator*()
   {
     return  T_Export(simplearray);
   }

   Array<T,1> simplearray;
};

 template<class T, int n>
  static Exporter<T,n>::T_Export member( Array<T, n> & _input) {
     return  Exporter<T,n>::T_Export(_input);
 }

int main()
{

   Array<double, 2> vector(8,1);
   vector  = 1.0,-1.0, 1.0, -1.0, 1.0 , 1.0, 1.0, -1.0;   
   Array<double, 2> result = Functions::Spectral::Walsh::Transform(vector);
   char c;
   //Array<Sample,1> arr_test(8);

   //Array<int , 1> arr_a = arr_test->a();
   Test<Sample, 1> q;
   Array<int, 1>  arr_a = (*q).a();

   //std::vector<int> A(3);
  // A  =  {0, 4 , 5};

  //result[ indexSet(A) ] = vector;
  Array<complex<double> , 2> fft_input(8,2);

   fft_input =  4,4,
               -3,-3,
                2,2 ,
                0,0 ,
               -1,-1,
               -2,-2,
                3,3,
                1,1
                   ;

   cout << fft_input;

   Array<double,2> a(3,3), b(3,3);

   a = 1, 5,2,
       1, 1,7,
       0, -3,4;

   b = a.copy();

   cout << a;

  Array<complex<double> , 2> fft_shuffle;
//  fft_shuffle = fft_input.copy();
  fft_shuffle.reference( Functions::Spectral::Fourier::Transform(fft_input));

  cout << "walsh" << result;

  cout << "walsh inverse";

  cout << Functions::Spectral::Walsh::InverseTransform(result);

   cout << fft_shuffle << "\n";
   cout << "inv" << Functions::Spectral::Fourier::InverseTransform(fft_shuffle) << "\n";
   cout << "end";

   //cout << result[ indexSet( A)] << "\n";

   // cin >> c;     
    Functions::Matrix::Inverse(a);
    cout << "Matrix_Inverse=" ;
    cout << a;

    Array<double, 2>  wavelet_input(8,2), wavelet_output;
    wavelet_input =  4,4,
                    -3,-3,
                     2,2 ,
                     0,0 ,
                     -1,-1,
                    -2,-2,
                    3,3,
                   1,1
                     ;

    Array<double,1>  filter;
    Functions::Spectral::Wavelet::Filter::MakeDaubechiesFilter(filter,4);
    cout << "out";
    cout << filter << "\n";

    wavelet_output.reference(Functions::Spectral::Wavelet::FWT_PO(wavelet_input,1,filter));

    cout << wavelet_output;

   Array<double,2>  back =  Functions::Spectral::Wavelet::IWT_PO(wavelet_output,1, filter);

   cout << back << "\n";

   Array<double,2> S ,D;
   Array<double,1> V;

   S.reference( b.copy());

   Functions::Matrix::SVD(S,V,D);

   /*  cout << "S=" << S << "\n"
          << "V=" << V << "\n"
          << "D=" << D ;
          */

   cout << Functions::Matrix::PesudoInverse(b, 0.00001);

   Array<double,2> lu_decomp(3,3);
   lu_decomp = a;
   cout << "lu = " << a;
   Functions::Matrix:: LUInverse(lu_decomp);

    cout   << "lu_decomp=" << lu_decomp;

     Array<double,2> fwt_mat_test(4,4), ifwt_mat_test;

     fwt_mat_test = 8* tensor::i + 4*tensor::j;
     cout << fwt_mat_test << "\n";

     ifwt_mat_test.reference(Functions::Spectral::Wavelet::FWT2_PO(fwt_mat_test, 1, filter));

     cout << ifwt_mat_test;

     fwt_mat_test.reference(Functions::Spectral::Wavelet::IWT2_PO(ifwt_mat_test, 1, filter));
       cout << fwt_mat_test;
     return 0;
}