double BLSSS::log_model_prob(const Selector &g)const{
    // borrowed from MLVS.cpp
    double num = vpri_->logp(g);
    if(num==BOOM::negative_infinity() || g.nvars() == 0) {
      // If num == -infinity then it is in a zero support point in the
      // prior.  If g.nvars()==0 then all coefficients are zero
      // because of the point mass.  The only entries remaining in the
      // likelihood are sums of squares of y[i] that are independent
      // of g.  They need to be omitted here because they are omitted
      // in the non-empty case below.
      return num;
    }
    SpdMatrix ivar = g.select(pri_->siginv());
    num += .5*ivar.logdet();
    if(num == BOOM::negative_infinity()) return num;

    Vector mu = g.select(pri_->mu());
    Vector ivar_mu = ivar * mu;
    num -= .5*mu.dot(ivar_mu);

    bool ok=true;
    ivar += g.select(suf().xtx());
    Mat L = ivar.chol(ok);
    if(!ok)  return BOOM::negative_infinity();
    double denom = sum(log(L.diag()));  // = .5 log |ivar|
    Vec S = g.select(suf().xty()) + ivar_mu;
    Lsolve_inplace(L,S);
    denom-= .5*S.normsq();  // S.normsq =  beta_tilde ^T V_tilde beta_tilde
    return num-denom;
  }
Example #2
0
void train_test(int nclasses, const Mat &train_data, const Mat &train_labels, const Mat &test_data, const Mat &test_labels, Mat &confusion) {
    // setup the ann:
    int nfeatures = train_data.cols;
    Ptr<ml::ANN_MLP> ann = ml::ANN_MLP::create();
    Mat_<int> layers(4,1);
    layers(0) = nfeatures;     // input
    layers(1) = nclasses * 8;  // hidden
    layers(2) = nclasses * 4;  // hidden
    layers(3) = nclasses;      // output, 1 pin per class.
    ann->setLayerSizes(layers);
    ann->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM,0,0);
    ann->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 300, 0.0001));
    ann->setTrainMethod(ml::ANN_MLP::BACKPROP, 0.0001);

    // ann requires "one-hot" encoding of class labels:
    Mat train_classes = Mat::zeros(train_data.rows, nclasses, CV_32FC1);
    for(int i=0; i<train_classes.rows; i++)
    {
        train_classes.at<float>(i, train_labels.at<int>(i)) = 1.f;
    }
    cerr << train_data.size() << " " << train_classes.size() << endl;

    ann->train(train_data, ml::ROW_SAMPLE, train_classes);

    // run tests on validation set:
    for(int i=0; i<test_data.rows; i++) {
        int pred  = ann->predict(test_data.row(i), noArray());
        int truth = test_labels.at<int>(i);
        confusion.at<int>(pred, truth) ++;
    }
    Mat correct = confusion.diag();
    float accuracy = sum(correct)[0] / sum(confusion)[0];
    cerr << "accuracy: " << accuracy << endl;
    cerr << "confusion:\n" << confusion << endl;
}
      void expectedMatDiag() {
        if(_colInd >= _genMat.n_cols) {
          return;
        }

        cout << "- Compute expectedMatDiag() ... ";
        save<double>("Mat.diagSuper", _genMat.diag(_colInd));
        cout << "done." << endl;
      }
      void expectedMatDiagElemDivide() {
        if(_rowInd >= _genMat.n_rows) {
          return;
        }

        if (_genRowVec.n_elem != min(_genMat.n_rows - _rowInd, _genMat.n_cols)) {
          return;
        }

        cout << "- Compute expectedMatDiagElemDivide() ... ";

        _genMat.diag(-_rowInd) /= _genRowVec;
        save<double>("Mat.diagSubElemDivide", _genMat);

        cout << "done." << endl;
      }
Example #5
0
  double BM::Loglike(const Vector &ab, Vec &g, Mat &h, uint nd) const{
    if (ab.size() != 2) {
      report_error("Wrong size argument.");
    }
    double alpha = ab[0];
    double beta = ab[1];
    if (alpha <= 0 || beta <= 0) {
      if (nd > 0) {
        g[0] = (alpha <= 0) ? 1.0 : 0.0;
        g[1] = (beta <= 0) ? 1.0 : 0.0;
        if (nd > 1) {
          h = 0.0;
          h.diag() = -1.0;
        }
      }
      return negative_infinity();
    }

    double n = suf()->n();
    double sumlog = suf()->sumlog();
    double sumlogc = suf()->sumlogc();

    double ans = n*(lgamma(alpha + beta) - lgamma(alpha)-lgamma(beta));
    ans += (alpha-1)*sumlog + (beta-1)*sumlogc;

    if(nd>0){
      double psisum = digamma(alpha + beta);
      g[0] = n*(psisum-digamma(alpha)) + sumlog;
      g[1] = n*(psisum-digamma(beta)) + sumlogc;

      if(nd>1){
 	double trisum = trigamma(alpha+beta);
 	h(0,0) = n*(trisum - trigamma(alpha));
 	h(0,1) = h(1,0) = n*trisum;
 	h(1,1) = n*(trisum - trigamma(beta));}}
    return ans;
  }
Example #6
0
  //----------------------------------------------------------------------
  double LSB::log_model_prob(const Selector &g)const{
    double num = vs_->logp(g);
    if(num==BOOM::negative_infinity()) return num;

    Ominv = g.select(pri_->siginv());
    num += .5*Ominv.logdet();
    if(num == BOOM::negative_infinity()) return num;

    Vec mu = g.select(pri_->mu());
    Vec Ominv_mu = Ominv * mu;
    num -= .5*mu.dot(Ominv_mu);

    bool ok=true;
    iV_tilde_ = Ominv + g.select(suf()->xtx());
    Mat L = iV_tilde_.chol(ok);
    if(!ok)  return BOOM::negative_infinity();
    double denom = sum(log(L.diag()));  // = .5 log |Ominv|

    Vec S = g.select(suf()->xty()) + Ominv_mu;
    Lsolve_inplace(L,S);
    denom-= .5*S.normsq();  // S.normsq =  beta_tilde ^T V_tilde beta_tilde

    return num-denom;
  }
inline
bool
op_logmat_cx::apply_common(Mat< std::complex<T> >& out, Mat< std::complex<T> >& S, const uword n_iters)
  {
  arma_extra_debug_sigprint();
  
  typedef typename std::complex<T> eT;
  
  Mat<eT> U;
  
  const bool schur_ok = auxlib::schur(U,S);
  
  if(schur_ok == false)  { arma_extra_debug_print("logmat(): schur decomposition failed"); return false; }
  
//double theta[] = { 1.10e-5, 1.82e-3, 1.62e-2,               5.39e-2,               1.14e-1,               1.87e-1,               2.64e-1              };
  double theta[] = { 0.0,     0.0,     1.6206284795015624e-2, 5.3873532631381171e-2, 1.1352802267628681e-1, 1.8662860613541288e-1, 2.642960831111435e-1 };
  // theta[0] and theta[1] not really used
  
  const uword N = S.n_rows;
  
  uword p = 0;
  uword m = 6;
  
  uword iter = 0;
  
  while(iter < n_iters)
    {
    const T tau = norm( (S - eye< Mat<eT> >(N,N)), 1 );
    
    if(tau <= theta[6])
      {
      p++;
      
      uword j1 = 0;
      uword j2 = 0;
      
      for(uword i=2; i<=6; ++i)  { if( tau      <= theta[i])  { j1 = i; break; } }
      for(uword i=2; i<=6; ++i)  { if((tau/2.0) <= theta[i])  { j2 = i; break; } }
      
      // sanity check, for development purposes only
      arma_debug_check( (j2 > j1), "internal error: op_logmat::apply_direct(): j2 > j1" );
      
      if( ((j1 - j2) <= 1) || (p == 2) )  { m = j1; break; }
      }
    
    const bool sqrtmat_ok = op_sqrtmat_cx::apply_direct(S,S);
    
    if(sqrtmat_ok == false)  { arma_extra_debug_print("logmat(): sqrtmat() failed"); return false; }
    
    iter++;
    }
  
  if(iter >= n_iters)  { arma_debug_warn("logmat(): reached max iterations without full convergence"); }
  
  S.diag() -= eT(1);
  
  if(m >= 1)
    {
    const bool helper_ok = op_logmat_cx::helper(S,m);
    
    if(helper_ok == false)  { return false; }
    }
  
  out = U * S * U.t();
  
  out *= eT(eop_aux::pow(double(2), double(iter)));
  
  return true;
  }
Example #8
0
void testMiscellaneous()
{
    cout << std::setprecision(16);
    cout << "f(.3)=" << f(Complex(0.3)) << endl;
    Real h = 1e-20;
    cout << "f(.3 + i*h)/h=" << f(Complex(0.3,h)) / h << endl;

    cout << CNT< Mat<2,3, Vec<2> > >::getNaN() << endl;

    Mat<2,3, Vec<2, Mat<2,2,Complex> > > isThisNaN;
    cout << "isThisNan? " << isThisNaN << endl;

    const Complex mdc[] = {
        Complex(1.,2.),  Complex(3.,4.),   Complex(5.,6.),   Complex(7.,8.),
        Complex(9.,10.), Complex(10.,11.), Complex(.1,.26),  Complex(.3,.45),
        Complex(.5,.64), Complex(.7,.83),  Complex(.9,.102), Complex(.10,.111)   
    }; 

    cout << "*** TEST COMPLEX DOT PRODUCT ***" << endl;
    Vec<3,Complex> vdot(mdc), wdot(&mdc[3]);
    Row<3,Complex> rdot(mdc), sdot(&mdc[3]);
    cout << "v=" << vdot << " w=" << wdot << endl;
    cout << "r=" << rdot << " s=" << sdot << endl;
    cout << "v.normalize()=" << vdot.normalize() << endl;
    cout << "r.normalize()=" << rdot.normalize() << endl;


    cout << "--- dot() global function:dot(v,w), rw, vs, rs should be the same" << endl;
    cout << "vw=" << dot(vdot,wdot) << " rw" << dot(rdot,wdot) 
         << " vs" << dot(vdot,sdot) << " rs" << dot(rdot,sdot) << endl;
    cout << "--- dot operator* requires row*col meaning Hermitian transpose with sign changes" << endl;
    cout << "vw=" << ~vdot*wdot << " rw" << rdot*wdot
         << " vs" << ~vdot*~sdot << " rs" << rdot*~sdot << endl;

    cout << endl << "*** TEST COMPLEX OUTER PRODUCT ***" << endl;
    cout << "--- outer() global function:dot(v,w), rw, vs, rs should be the same" << endl;
    cout << "vw=" << outer(vdot,wdot) << " rw" << outer(rdot,wdot) 
         << " vs" << outer(vdot,sdot) << " rs" << outer(rdot,sdot) << endl;
    cout << "--- outer operator* requires col*row meaning Hermitian transpose with sign changes" << endl;
    cout << "vw=" << vdot*~wdot << " rw" << ~rdot*~wdot
         << " vs" << vdot*sdot << " rs" << ~rdot*sdot << endl;

    cout << "*** TEST COMPLEX CROSS PRODUCT ***" << endl;
    cout << "--- cross() global function:dot(v,w), rw, vs, rs should be the same" << endl;
    cout << "vw=" << cross(vdot,wdot) << " rw" << cross(rdot,wdot) 
         << " vs" << cross(vdot,sdot) << " rs" << cross(rdot,sdot) << endl;
    cout << "--- cross operator% involves NO sign changes, but returns row if either arg is a row" << endl;
    cout << "vw=" << vdot%wdot << " rw" << rdot%wdot
         << " vs" << vdot%sdot << " rs" << rdot%sdot << endl;

    cout << "*** TEST crossMat() ***" << endl;
    Mat<3,3,Complex> vcross(crossMat(vdot));
    Mat<3,3,Complex> rcross(crossMat(rdot));
    cout << "--- crossMat 3d should be same whether made from row or vec" << endl;
    cout << "vdot%wdot=" << vdot%wdot << endl;
    cout << "crossMat(v)=" << vcross << "crossMat(r)=" << rcross;
    cout << "crossMat(v)*w=" << crossMat(vdot)*wdot << " vcross*w=" << vcross*wdot << endl;


    Vec<2,Complex> vdot2 = vdot.getSubVec<2>(0);
    Vec<2,Complex> wdot2 = wdot.getSubVec<2>(0);
    Row<2,Complex> rdot2 = rdot.getSubRow<2>(0);
    Row<2,Complex> vcross2(crossMat(vdot2));
    Row<2,Complex> rcross2(crossMat(rdot2));

    cout << "--- crossMat 2d should be same whether made from row or vec" << endl;
    cout << "vdot2, wdot2=" << vdot2 << ", " << wdot2 << " vdot2%wdot2=" << vdot2%wdot2 << endl;
    cout << "crossMat(v2)=" << vcross2 << "crossMat(r2)=" << rcross2;
    cout << "crossMat(v2)*w2=" << crossMat(vdot2)*wdot2 << " vcross2*w2=" << vcross2*wdot2 << endl;

    cout << "*********\n";



    Mat<2,5,float> m25f( 1, 2, 3, 4, 5,
                         6, 7, 8, 9, 10 );
    cout << "Mat<2,5,float>=" << m25f;
    cout << "Mat<2,5,float>.normalize()=" << m25f.normalize();
    cout << "Mat<2,5,float>.sqrt()=" << m25f.sqrt();

    const Mat<1,5,Vec<2,float> >& m15v2f = 
        *reinterpret_cast<const Mat<1,5,Vec<2,float> >*>(&m25f);
    cout << "  m25f@" << &m25f << " m15v2f@" << &m15v2f << endl;
    cout << "Mat<1,5,Vec<2,float> >=" << m15v2f;;
    cout << "Mat<1,5,Vec<2,float> >.normalize()=" << m15v2f.normalize();

    const Real twoXthree[] = { 1, 2, 3,
                               4, 5, 6 };
    const Real threeXone[] = { .1, .001, .00001 };
    const Real sym33[] = { 1,
                           2, 3,
                           4, -5, 6 };
    SymMat<3> sm3(sym33);
    cout << "SymMat<3> sm3=" << sm3;
    Mat<3,3> m33sm3;
    for (int i=0; i<3; ++i) 
        for (int j=0; j<=i; ++j)
            m33sm3(i,j) = m33sm3(j,i) = sm3(i,j);
    cout << "Mat33(sm3)=" << m33sm3;
    cout << "sm3*3=" << sm3*3;
    cout << "sm3+100=" << sm3+100;
    cout << "m33sm3+100=" << m33sm3+100;
    cout << "sm3.normalize()=" << sm3.normalize();
    cout << "m33sm3.normalize()=" << m33sm3.normalize();
    cout << "sm3+=100:" << (sm3+=100.);
    cout << "m33sm3+=100:" << (m33sm3+=100.);

    Mat<3,3,Complex> whole(mdc);
    SymMat<3,Complex,9> sym = SymMat<3,Complex,9>().setFromLower(whole);
    cout << "whole=" << whole << endl;
    cout << "sym  =" << sym << "(pos~)sym  =" << sym.positionalTranspose() << endl;

    cout << "whole.real()=" << whole.real();
    cout << "whole.imag()=" << whole.imag();
    cout << "sym.real()=" << sym.real();
    cout << "sym.imag()=" << sym.imag();



    Mat<3,4,Complex>  mdcp(mdc);  cout << "*** Data looks like this: " << mdcp;
    SymMat<4,negator<Complex> > symp(reinterpret_cast<const negator<conjugate<double> >*>(mdc));
    cout << "    4x4 Sym<Neg<cmplx>> from (negator<conj>)pointer to data gives this:" << symp;
    cout << "    sym.real()=" << symp.real();
    cout << "    sym.imag()=" << symp.imag();
    cout << "   ~sym.imag()=" << ~symp.imag();
    cout << "pos~(sym.imag())=" << symp.imag().positionalTranspose();
    cout << "(pos~sym).imag()=" << symp.positionalTranspose().imag();
    cout << "   -sym.imag()=" << -symp.imag();

    symp(2,1).real() = 99.;
    cout << "after sym(2,1).real=99, sym=" << symp;

    symp.updPositionalTranspose().imag()(3,1)=123.;
    cout << "after (pos~sym).imag()(3,1)=123, (pos~sym).imag()=" << symp.positionalTranspose().imag();
    cout << "    ... sym=" << symp;

    Mat<2,3, SymMat<3,Complex> > weird(Row<3,SymMat<3,Complex> >( sym, -sym, sym ),
                                       Row<3,SymMat<3,Complex> >( sym, sym, sym ));
    cout << "weird=" << weird;
    weird *= 2.;
    cout << " weird*=2: " << weird;
    cout << " weird(1)=" << weird(1) << endl;
    cout << " weird(0,1)=" << weird(0,1) << " [0][1]=" << weird[0][1] << endl;

    cout << " typename(weird)=" << typeid(weird).name() << endl;
    cout << " typename(weird.real)=" << typeid(weird.real()).name() << endl;
    cout << " typename(weird.imag)=" << typeid(weird.imag()).name() << endl;

    cout << " weird.real()=" << weird.real();
    cout << " weird.imag()=" << weird.imag();

    cout << "sizeof(sym<3,cplx>)=" << sizeof(sym) << " sizeof(mat<2,3,sym>=" << sizeof(weird) << endl;

    Mat<2,3> m23(twoXthree);
    Mat<3,1> m31(threeXone);
    cout << "m23=" << m23 << endl;
    cout << "m31=" << m31 << endl;
    cout << "m23*-m31=" << m23*-m31 << endl;
    cout << "~ ~m31 * ~-m23=" << ~((~m31)*(~-m23)) << endl;

    Mat<2,3,Complex> c23(m23);
    Mat<3,1,Complex> c31(m31);
    cout << "c23=" << c23 << endl;
    cout << "c31=" << c31 << endl;
    cout << "c23*c31=" << c23*-c31 << endl;
    cout << "  ~c31 * ~-c23=" << (~c31)*(~-c23) << endl;
    cout << "~ ~-c31 * ~c23=" << ~((~-c31)*(~c23)) << endl;


    Mat<3,4> m34;
    Mat<3,4,Complex> cm34;


    cm34 = mdc;
    m34 = cm34.real();

    cout << "Mat<3,4,Complex> cm34=" << cm34 << endl;
    cout << "cm34.diag()=" << cm34.diag() << endl;

    cout << "cm34 + cm34=" << cm34+cm34 << endl; //INTERNAL COMPILER ERROR IN Release MODE
    cout << "~cm34 * 1000=" << ~cm34 * 1000. << endl;

    cout << "m34=" << m34 << endl;
    m34 =19.123;
    cout << "after m34=19.123, m34=" << m34 << endl;
 
    const double ddd[] = { 11, 12, 13, 14, 15, 16 }; 
    const complex<float> ccc[] = {  complex<float>(1.,2.),  
                                    complex<float>(3.,4.),
                                    complex<float>(5.,6.),
                                    complex<float>(7.,8.) };
    Vec<2,complex<float>,1> cv2(ccc);
    cout << "cv2 from array=" << cv2 << endl;
    cv2 = Vec<2,complex<float> >(complex<float>(1.,2.), complex<float>(3.,4.));
    cout << "cv2 after assignment=" << cv2 << endl;

    cout << "cv2.real()=" << cv2.real() << " cv2.imag()=" << cv2.imag() << endl;

    Vec<2,negator<complex<float> >,1>& negCv2 = (Vec<2,negator<complex<float> >,1>&)cv2;
    Vec<2,conjugate<float>,1>& conjCv2 = (Vec<2,conjugate<float>,1>&)cv2;
    Vec<2,negator<conjugate<float> >,1>& negConjCv2 = (Vec<2,negator<conjugate<float> >,1>&)cv2;

    

    Vec<2,complex<float> > testMe = cv2;
    cout << "testMe=cv2 (init)=" << testMe << endl;
    testMe = cv2;
    cout << "testMe=cv2 (assign)=" << testMe << endl;


    cout << "(cv2+cv2)/complex<float>(1000,0):" << (cv2 + cv2) / complex<float>(1000,0) << endl; 
    cout << "(cv2+cv2)/1000.f:" << (cv2 + cv2) / 1000.f << endl;
    cout << "(cv2+cv2)/1000.:" << (cv2 + cv2) / 1000. << endl;
    cout << "(cv2+cv2)/1000.L:" << (cv2 + cv2) / 1000.L << endl;
    cout << "(cv2+cv2)/1000:" << (cv2 + cv2) / 1000 << endl;

    cout << "negCv2=" << negCv2 << endl;
    cout << "conjCv2=" << conjCv2 << endl;
    cout << "negConjCv2=" << negConjCv2 << endl;
    cout << "cv2+negCv2=" << cv2+negCv2 << endl;

    negConjCv2 = complex<float>(8,9);
    cout << "AFTER negConjCv2 = (8,9):" << endl;
    cout << "  cv2=" << cv2 << endl;
    cout << "  negCv2=" << negCv2 << endl;
    cout << "  conjCv2=" << conjCv2 << endl;
    cout << "  negConjCv2=" << negConjCv2 << endl;

    cout << "cv2:  " << cv2 << endl;
    cout << "cv2T: " << cv2.transpose() << endl; 
    cout << "-cv2: " << -cv2 << endl;
    cout << "~cv2: " << ~cv2 << endl;
    cout << "-~cv2: " << -(~cv2) << endl;
    cout << "~-cv2: " << ~(-cv2) << endl; 
    cout << "~-cv2*10000: " << (~(-cv2))*10000.f << endl;  
        
   (~cv2)[1]=complex<float>(101.1f,202.3f);
    cout << "after ~cv2[1]=(101.1f,202.3f), cv2= " << cv2 << endl;    
    (-(~cv2))[1]=complex<float>(11.1f,22.3f);
    cout << "after -~cv2[1]=(11.1f,22.3f), cv2= " << cv2 << endl; 
        
    Vec<3> dv3(ddd), ddv3(ddd+3);
    dv3[2] = 1000;
    cout << "dv3=" << dv3 << " ddv3=" << ddv3 << endl;
    cout << "100(ddv3-dv3)/1000=" << 100.* (ddv3 - dv3) / 1000. << endl; 

    Vec<3> xxx(dv3); cout << "copy of dv3 xxx=" << xxx << endl;
    Vec<3> yyy(*ddd);cout << "copy of *ddd yyy=" << yyy << endl;
    
    cout << "dv3.norm()=" << dv3.norm() << endl;
    cout << "cv2=" << cv2 << " cv2.norm()=" << cv2.norm() << endl; 
       
    const Vec<2> v2c[] = {Vec<2>(ddd),Vec<2>(ddd+1)};
    Vec<2, Vec<2> > vflt(v2c);
    cout << "vflt 2xvec2=" << vflt << endl;
    cout << "10.*vflt=" << 10.*vflt << endl;
    cout << "vflt*10.=" << vflt*10. << endl;

    int ivals[] = {0x10, 0x20, 0x30, 0x40};
    Vec<4> iv(ivals);
    cout << "iv=" << iv << endl;
    
    Vec<2, Vec<2> > v22;
    v22 = Vec<2>(&ivals[2]);
    cout << "v22=" << v22 << endl;


    // Test dot product
    {
    double d[] = {1,2,3,4,5,6,7,8};


    Vec<2> v1(&d[0]), v2(&d[2]);
    Row<2> r1(&d[4]), r2(&d[6]);
    Vec<2>::TNeg& nv1 = (Vec<2>::TNeg&)v1;

    negator<double> nd(100); cout << endl << "nd=" << nd << endl;
    cout << "nv1=" << nv1 << endl;
    cout << "nv1*nd=" << nv1*nd << endl;
    cout << "nd*nv1=" << nd*nv1 << endl;
    cout << "nv1/nd=" << nv1/nd << endl << endl;


    cout << "v1,v2=" << v1 << v2 << endl;
    cout << "r1,r2=" << r1 << r2 << endl;
    cout << "dot r1*v1 =" << dot(r1,v1) << endl;
    cout << "dot r1*nv1=" << dot(r1,nv1) << endl;
    cout << "r1*v1 =" << r1*v1 << endl;
    cout << "r1*nv1=" << r1*nv1 << endl;
    
    // outer product
    cout << " outer v1*r1=" << v1*r1 << endl;
    cout << " outer nv1*r1=" << nv1*r1 << endl;

    // cross product (2d)
    cout << "cross(v1,v2)=" << cross(v1,v2) << endl;
    cout << "v1 % v2=" << v1 % v2 << endl;
    cout << "cross(r1,v2)=" << cross(r1,v2) << endl;
    cout << "r1 % v2=" << r1 % v2 << endl;
    cout << "cross(v1,r2)=" << cross(v1,r2) << endl;
    cout << "v1 % r2=" << v1 % r2 << endl;
    cout << "cross(r1,r2)=" << cross(r1,r2) << endl;
    cout << "r1 % r2=" << r1 % r2 << endl;

    // do the cross products with 3d routines
    Vec3 v13(v1[0],v1[1],0), v23(v2[0],v2[1],0);
    Row3 r13(r1[0],r1[1],0), r23(r2[0],r2[1],0);
    cout << "cross(v13,v23)=" << cross(v13,v23) << endl;
    cout << "v13 % v23=" << v13 % v23 << endl;
    cout << "cross(r13,v23)=" << cross(r13,v23) << endl;
    cout << "r13 % v23=" << r13 % v23 << endl;
    cout << "cross(v13,r23)=" << cross(v13,r23) << endl;
    cout << "v13 % r23=" << v13 % r23 << endl;
    cout << "cross(r13,r23)=" << cross(r13,r23) << endl;
    cout << "r13 % r23=" << r13 % r23 << endl;

    v13[2]=7;
    cout << "v13=" << v13 << " 2*v13+.001=" << 2*v13+.001 << endl;
    cout << "v13 % (2*v13)=" << v13 % (2*v13) << endl;
    cout << "v13 % (2*v13+.001)=" << v13 % (2*v13+.001) << endl;

    cout << endl;

    // test constructors
    Mat<2,3> mvcols( v1, ~r1, v2 );
    Mat<3,2> mvrows( ~v1, 
                      r1,
                      r2 ); 

    cout << "mvcols=" << mvcols << endl;
    cout << "mvrows=" << mvrows << endl;   

    Vec<3,float> v2f(39.f, 40.f, 50.L);
    cout << "v2f=" << v2f << endl;

    cout << "v2f.drop1(0)=" << v2f.drop1(0) << endl;
    cout << "v2f.drop1(1)=" << v2f.drop1(1) << endl;
    cout << "v2f.drop1(2)=" << v2f.drop1(2) << endl;

    cout << "v2f.append1(3.3f)=" << v2f.append1(3.3f) << endl;
    cout << "v2f.append1((short)1)="   << v2f.append1((short)1)   << endl;
    cout << "v2f.drop1(1).append1((unsigned short)0x10)=" << v2f.drop1(1).append1((unsigned short)0x10) << endl;
    cout << "(~v2f).drop1(1).append1((unsigned short)0x10)=" << (~v2f).drop1(1).append1((unsigned short)0x10) << endl;

    cout << "v2f.insert1(0, 23.f)=" << v2f.insert1(0, 23.f) << endl;
    cout << "v2f.insert1(1, 23.f)=" << v2f.insert1(1, 23.f) << endl;
    cout << "v2f.insert1(2, 23.f)=" << v2f.insert1(2, 23.f) << endl;
    cout << "v2f.insert1(3, 23.f)=" << v2f.insert1(3, 23.f) << endl;
    cout << "v2f.insert1(2, 23.f).drop1(2)=" << v2f.insert1(2, 23.f).drop1(2) << endl;

    cout << "(~v2f).insert1(2, 23.f).drop1(2)=" << (~v2f).insert1(2, 23.f).drop1(2) << endl;

    Mat33 m33( Row3(1,     2,     3),
               Row3(4,     5,     6),
               Row3(.003f, 9.62L, 41.1) );
    cout << "m33=" << m33 << endl;  
    cout << "v13=" << v13 << endl;
    cout << "m33*v13=" << m33*v13 << endl;
    cout << "~m33*v13=" << (~m33)*v13 << endl;
    cout << "~v13*~m33=" << ~v13*(~m33) << endl;
    }

    Mat<4,3> ident43;
    ident43 = 1;
    cout << "ident43=" << ident43 << endl;

    Mat<4,3> negid43 = -ident43; // requires implicit conversion from Mat<negator<Real>>
    cout << "negid43=" << negid43 << endl;

    // Absolute value
    const Vec<3> vorig(1.,-2.,-3.);
    cout << "vorig=" << vorig << " vorig.abs()=" << vorig.abs() << endl;
    const Vec<2, Vec<3> > v2orig(vorig,-vorig);
    cout << "v2orig=" << v2orig << " v2orig.abs()=" << v2orig.abs() << endl;

    Vec<3> nvorig = -vorig;
    Vec<2, Vec<3> > nv2orig = -v2orig;

    Mat<3,2> morig(vorig,-vorig);
    cout << "morig=" << morig << " morig.abs()=" << morig.abs() << endl;

    //Mat<2,2, Vec<3> > m2orig(v2orig, (Vec<2, Vec<3> >)-v2orig);
    //cout << "m2orig=" << m2orig << " m2orig.abs()=" << m2orig.abs() << endl;

    cout << "vorig.getSubVec<2>(1)=" << vorig.getSubVec<2>(1) << endl;

    negid43.updSubMat<2,2>(2,1) = -27.;
    cout << "after negid43.updSubMat<2,2>(2,1) = -27., negid43=" << negid43;

    cout << "negid43[2].getSubRow<2>(1)=" << negid43[2].getSubRow<2>(1) << endl;

    cout << "CHECK DIAGONAL LENGTH FOR RECTANGULAR MATRICES" << endl;
    Mat<3,2, Row3, 1, 2> H;
    cout << "H[" << H.nrow() << "," << H.ncol() << "]" << endl;
    cout << "H.diag()[" << H.diag().nrow() << "," << H.diag().ncol() << "]" << endl;
    Mat<3,2, Row3, 1, 2>::TransposeType Ht;
    cout << "Ht[" << Ht.nrow() << "," << Ht.ncol() << "]" << endl;
    cout << "Ht.diag()[" << Ht.diag().nrow() << "," << Ht.diag().ncol() << "]" << endl;
}
Example #9
0
    //Class constructor
	TimeSegmentation(Tobj &G, Col <T1> map_in, Col <T1> timeVec_in, uword a, uword b, uword c, uword interptype = 1,
					 uword shots = 1)
    {
		cout << "Entering Class constructor" << endl;
	    n1 = a; //Data size
	    n2 = b;//Image size
	    L = c; //number of time segments
	    type = interptype; // type of time segmentation performed
	    Nshots = shots; // number of shots
	    obj = &G;
	    fieldMap = map_in;
    	cout << "N1 = " << n1 << endl;
		cout << "N2 = " << n2 << endl;
		cout << "L = " << L << endl;


		AA.set_size(n1, L); //time segments weights
	    timeVec = timeVec_in;
	    T_min =timeVec.min();
	    T1 rangt = timeVec.max()-T_min;
		tau = (rangt + datum::eps) / (L - 1); // it was L-1 before
	    timeVec = timeVec-T_min;

	    uword NOneShot = n1/Nshots;
	    if (L==1) {
		    tau = 0;
		    AA.ones();
	    }
	    else {
			Mat <CxT1> tempAA(NOneShot, L);
		    if (type==1) {// Hanning interpolator
			    cout << "Hanning interpolation" << endl;
			    for (unsigned int ii = 0; ii<L; ii++) {
				    for (unsigned int jj = 0; jj<NOneShot; jj++) {
					    if ((std::abs(timeVec(jj)-((ii)*tau)))<=tau) {
						    tempAA(jj, ii) = 0.5+0.5*std::cos((datum::pi)*(timeVec(jj)-((ii)*tau))/tau);
					    }
					    else {
						    tempAA(jj, ii) = 0.0;
					    }
				    }
			    }
			    AA = repmat(tempAA, Nshots, 1);
		    }
		    else if (type==2) { // Min-max interpolator: Exact LS interpolator

			    cout << "Min Max time segmentation" << endl;

			    Mat <CxT1> Ltp;
			    Ltp.ones(1, L);
			    Col <CxT1> ggtp;
			    ggtp.ones(n2, 1);
			    Mat <CxT1> gg;
			    gg = exp(i*fieldMap*tau)*Ltp;
			    Mat <CxT1> iGTGGT;
			    iGTGGT.set_size(L+1, n2);
			    Mat <CxT1> gl;
			    gl.zeros(n2, L);


			    for (unsigned int ii = 0; ii<L; ii++) {
				    for (unsigned int jj = 0; jj<n2; jj++) {
					    gl(jj, ii) = pow(gg(jj, ii), (T1) (ii+1));
				    }
			    }

			    Mat <CxT1> G;
			    G.set_size(n2, L);

			    for (unsigned int jj = 0; jj<L; jj++) {
				    if (jj==0) {
					    G.col(jj) = ggtp;
				    }
				    else {
					    G.col(jj) = gl.col(jj-1);
				    }
			    }

			    Col <CxT1> glsum;
			    Mat <CxT1> GTG;
			    GTG.zeros(L, L);
			    GTG.diag(0) += n2;
			    glsum = sum(gl.t(), 1);
				Mat <CxT1> GTGtp(L, L);
				for (unsigned int ii = 0; ii < (L - 1); ii++) {
					GTGtp.zeros();
				    GTGtp.diag(-(T1) (ii+1)) += glsum(ii);
				    GTGtp.diag((T1) (ii+1)) += std::conj(glsum(ii));
				    GTG = GTG+GTGtp;
			    }

			    T1 rcn = 1/cond(GTG);
			    if (rcn>10*2e-16) { //condition number of GTG
				    iGTGGT = inv(GTG)*G.t();

			    }
			    else {
				    iGTGGT = pinv(GTG)*G.t(); // pseudo inverse
			    }


			    Mat <CxT1> iGTGGTtp;
			    Mat <CxT1> ftp;
			    Col <CxT1> res, temp;

			    for (unsigned int ii = 0; ii<NOneShot; ii++) {
				    ftp = exp(i*fieldMap*timeVec(ii));
				    res = iGTGGT*ftp;
				    tempAA.row(ii) = res.t();
			    }
			    AA = repmat(tempAA, Nshots, 1);
		    }
	    }
		savemat("aamat.mat", "AA", vectorise(AA));
		cout << "Exiting class constructor." << endl;
    }