Пример #1
0
/**
 * returns a diagonal matrix
 * @param  v a vector
 * @return a diagonal matrix with the given vector v on the diagonal
 */
Matrix Diag(const Matrix& v)
{
  Matrix res;
  if (v.GetCols() == 1)
  {
    // the given matrix is a vector n x 1
    int rows = v.GetRows();
    res = Matrix(rows, rows);

    // copy the values of the vector to the matrix
    for (int r=1; r <= rows; r++)
    {
      res(r, r) = v.get(r, 1);
    }
  }
  else if (v.GetRows() == 1)
  {
    // the given matrix is a vector 1 x n
    int cols = v.GetCols();
    res = Matrix(cols, cols);

    // copy the values of the vector to the matrix
    for (int c=1; c <= cols; c++)
    {
      res(c, c) = v.get(1, c);
    }
  }
  else
  {
    throw Exception("Parameter for diag must be a vector");
  }
  return res;
}
Пример #2
0
norm_tools::dist_stat calc_dist_matrix_impl(const Matrix& data, Matrix& dist, norm_tools::vv_norm_fn_t norm2_fn)
{
	//some constant values used
	const ulong points_num = data.row_num();
	const double mult = 2.0/(points_num * (points_num - 1));
	const double mult1 = 1.0/(points_num - 1);

	//localy used matrices
	Matrix dv, norm; //, dist_row(1, points_num);
	//statistics
	norm_tools::dist_stat stat;
	//meand distance^2 & meand distance^2 between nearest neighbours
	double meand2 = 0, meand2_nn = 0;
	//current distance & minimum distances
	double cur_dist, cur_mind = 0, cur_mind2 = 0;

	//resize distance matrix
	dist.Resize(points_num, points_num);
	//zero distance matrix
	dist = 0;
	//start distances calculation
	for(ulong i = 0; i < points_num - 1; ++i) {
		dv <<= data.GetRows(i);
		//calc dist^2 to all other rows
		for(ulong j = i + 1; j < points_num; ++j) {
			//calc dist^2
			//norm <<= dv - data.GetRows(j);
			//transform(norm, norm, multiplies<double>());
			//cur_dist = distance^2
			//cur_dist = norm.Sum();
			cur_dist = norm2_fn(dv, data.GetRows(j));

			//update mean of distance^2
			meand2 += mult * cur_dist;
			//update current min distance^2
			if(j == i + 1 || cur_dist < cur_mind2) cur_mind2 = cur_dist;
			//cur_dist = pure l2 distance
			cur_dist = sqrt(cur_dist);
			//update current minimum distance (to nearest neighbour)
			if(j == i + 1 || cur_dist < cur_mind) cur_mind = cur_dist;
			//save it into matrix elements (i,j) and (j, i)
			dist(i, j) = cur_dist; dist(j, i) = cur_dist;
			//update global mean distance
			stat.mean_ += cur_dist * mult;
		}
		//update global min distance
		if(i == 0 || cur_mind < stat.min_) stat.min_ = cur_mind;
		//update mean nearest neighbour distance
		stat.mean_nn_ += cur_mind * mult1;
		//update mean nearest neighbour distance^2
		meand2_nn += cur_mind2 * mult1;
	}

	//calc mse
	stat.mse_ = sqrt(stat.mean_*(meand2/stat.mean_ - stat.mean_));
	stat.mse_nn_ = sqrt(stat.mean_nn_*(meand2_nn/stat.mean_nn_ - stat.mean_nn_));
	return stat;
}
Пример #3
0
// / Calculates the mean value of the elements in a matrix.
double mean(Matrix < double >&m)
{
    double sum = 0.;

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += m[r][c];
    return sum / double (m.GetRows() * m.GetCols());
}
Пример #4
0
Complex scalprod(Matrix < Complex > &m1, Matrix < double >&m2)
{
    assert((m1.GetRows() == m2.GetRows()) && (m1.GetCols() == m2.GetCols()));
    Complex sum(0, 0);

    for (int r = 0; r < m1.GetRows(); r++)
        for (int c = 0; c < m1.GetCols(); c++)
            sum += m1[r][c] * m2[r][c];
    return sum;
}
Пример #5
0
// / Calculates the varaince of the elements in a matrix.
double variance(Matrix < double >&m)
{
    double sum = 0.;

    double average = mean(m);

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += (m[r][c] - average) * (m[r][c] - average);
    return sum / double (m.GetRows() * m.GetCols() - 1);
}
Пример #6
0
Complex scalconj(Matrix < Complex > &m1, Matrix < Complex > &m2)        // total(A.B*)
{
    assert((m1.GetRows() == m2.GetRows()) && (m1.GetCols() == m2.GetCols()));
    Complex sum(0, 0);

    for (int r = 0; r < m1.GetRows(); r++)
        for (int c = 0; c < m1.GetCols(); c++)
            sum += m1[r][c] * conj(m2[r][c]);
    return sum;

}
Пример #7
0
void GA::nn_addon::_build_surf(ulong net_ind, const Matrix& input, const Matrix& targets)
{
	/*
	Matrix x(1, 100), y(100, 1);
	x = abs(_ga.opt_.initRange(1, 0) - _ga.opt_.initRange(0, 0))/(x.size() - 1);
	x[0] = 0; x <<= !(!x).CumSum(); x += _ga.opt_.initRange(0, 0);
	y = abs(_ga.opt_.initRange(1, 1) - _ga.opt_.initRange(0, 1))/(y.size() - 1);
	y[0] = 0; y <<= y.CumSum(); y += _ga.opt_.initRange(0, 1);

	Matrix yrow(1, 100);
	Matrix surf;
	for(ulong i = 0; i < y.row_num(); ++i) {
		yrow = y[i];
		if(opt_.netType == matrix_nn)
			surf &= _net.Sim(x & yrow);
		else
			surf &= _onet[net_ind]->sim(x & yrow);
	}l
	DumpMatrix(x, "x.txt");
	DumpMatrix(y, "y.txt");
	DumpMatrix(surf, "z.txt");
	*/

	Matrix points(ga_.opt_.initRange.col_num(), 10000);
	generate(points.begin(), points.end(), prg::rand01);
	const ulong pnum = points.col_num();
	//range matrix
	Matrix range = input.minmax(false);
	//lower bound
	Matrix a = range.GetRows(0);
	//difference between bounds
	Matrix scale = range.GetRows(1) - a;
	//start moving points
	Matrix::r_iterator pos = points.begin();
	Matrix::r_iterator p_a = a.begin();
	Matrix::r_iterator p_sc = scale.begin();
	for(ulong i = 0; i < points.row_num(); ++i, ++p_a, ++p_sc)
	{
		//x = x*(b - a)
		transform(pos, pos + pnum, pos, bind2nd(multiplies<double>(), *p_sc));
		pos = transform(pos, pos + pnum, pos, bind2nd(plus<double>(), *p_a));
		//pos += points.col_num();
	}
	DumpMatrix(points, "y.txt");

	Matrix surf;
	if(opt_.netType == matrix_nn)
		surf <<= net_.Sim(points);
	else
		surf <<= _onet[net_ind]->sim(points);
	DumpMatrix(surf, "z.txt");
}
Пример #8
0
Matrix nn_addon::_best_filter(const Matrix& p, const Matrix& f, Matrix& lp, Matrix& lf)
{
	lf = f;
	indMatrix mInd = lf.RawSort();
	lp.NewMatrix(min<ulong>(opt_.bestCount, p.row_num()), p.col_num());
	for(ulong i = 0; i < lp.row_num(); ++i)
		lp.SetRows(p.GetRows(mInd[i]), i);
	if(lp.row_num() < lf.row_num())
		lf.DelRows(lp.row_num(), lf.row_num() - lp.row_num());

	if(opt_.search_samples_num > 0)
		return lp.GetRows(0, opt_.search_samples_num).minmax();
	else
		return lp.minmax();
}
Пример #9
0
Matrix Matrix::operator*(Matrix oper)
{
	float *B = oper.GetMatrix();
	int RB = oper.GetRows();
	int CB = oper.GetColumns();
	Matrix C(oper.GetColumns(), GetColumns());
	int CSize = C.GetColumns()*C.GetRows();
	int Columns = C.GetColumns();
	float *Result = C.GetMatrix();

	Result[0] = A[0]*B[0] + A[1]*B[3] + A[2]*B[6]; 
	Result[1] = A[0]*B[1] + A[1]*B[4] + A[2]*B[7]; 
	Result[2] = A[0]*B[2] + A[1]*B[5] + A[2]*B[8]; 

	Result[3] = A[3]*B[0] + A[4]*B[3] + A[5]*B[6]; 
	Result[4] = A[3]*B[1] + A[4]*B[4] + A[5]*B[7]; 
	Result[5] = A[3]*B[2] + A[4]*B[5] + A[5]*B[8]; 

	Result[6] = A[6]*B[0] + A[7]*B[3] + A[8]*B[6]; 
	Result[7] = A[6]*B[1] + A[7]*B[4] + A[8]*B[7]; 
	Result[8] = A[6]*B[2] + A[7]*B[5] + A[8]*B[8]; 
	

	return C;
}
Пример #10
0
Matrix fft2(Matrix &m, int isign)
{
    assert(m.bComplex == TRUE);
    int ndim = 2;	
    long row = m.GetRows(); 
    long col = m.GetCols();
	
    Matrix ret(m);
    unsigned long nn[3];
		
    nn[1] = row;
    nn[2] = col / 2;
		
#if 1
    float *pdata = new float[row * col + 1];
    memcpy( pdata + 1, m.p, sizeof(float) * row * col);
    fourn( pdata, nn, ndim, isign );
    memcpy( ret.p, pdata + 1, sizeof(float) * row * col);
    delete[] pdata;
#else
	float* pdata = new float[row * col];
	memcpy(pdata, m.p, sizeof(float) * row * col);
	CUFFT::fft(pdata, col / 2, row, isign == 1);
	CUFFT::fft(pdata, col / 2, row, isign != 1);
	memcpy(ret.p, pdata, sizeof(float) * row * col);
	delete [] pdata;
#endif
    ret.bComplex = TRUE;
		
    return ret;	
}
Пример #11
0
Matrix WhitenFrame(Matrix &m)
{
    int nRow = m.GetRows();
    int nCol = m.GetCols();
    assert(nRow == nCol);
	
    Matrix fx(nRow), fy(nRow);
    Meshgrid(fx, -nRow / 2, nRow / 2 - 1, fy, -nRow / 2, nRow / 2 - 1);
    Matrix theta = fx;
    Matrix rho = fx;
    
    cart2pol( fx,fy,theta,rho );
	
#if 1
    Matrix complex = Complex(m);
    Matrix fftm = fft2(complex, 1);
    Matrix imF = fftshift(fftm);
    Matrix time = times(rho, imF);
    Matrix imW = fftshift(time);
#else
    Matrix complex = Complex(m);
    Matrix fftm = fft2(complex, 1);
    Matrix imF = fftshift(fftm);
    Matrix time = times(rho, imF);
    Matrix imW = fftshift(time);
#endif

    return imW;
}
Пример #12
0
Matrix nn_addon::GetChromMM(const Matrix& pop)
{
	Matrix res(pop.row_num(), 2);
	for(ulong i=0; i<pop.row_num(); ++i) {
		res(i, 0) = pop.GetRows(i).Min();
		res(i, 1) = pop.GetRows(i).Max();
	}
	return res;
}
Пример #13
0
Complex total(Matrix < Complex > &m)
{
    Complex sum(0, 0);

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += m[r][c];
    return sum;
}
Пример #14
0
// / Computes the sum of all of the elements in a matrix.
double total(Matrix < double >&m)
{
    double sum = 0.;

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += m[r][c];
    return sum;

}
Пример #15
0
double norm2(Matrix < Complex > &m)
{
    double sum = 0.;

    for (int r = 0; r < m.GetRows(); r++)
        for (int c = 0; c < m.GetCols(); c++)
            sum += norm(m[r][c]);
    return sum;

}
Пример #16
0
template< > _CLASS_DECLSPEC
Matrix norm_tools::vm_norm2< norm_tools::l2 >(const Matrix& v_from, const Matrix& m_to) {
	Matrix norm(1, m_to.row_num());
	Matrix diff;
	for(ulong i = 0; i < norm.size(); ++i) {
		diff <<= v_from - m_to.GetRows(i);
		norm[i] = diff.Mul(diff).Sum();
	}
	return norm;
}
Пример #17
0
template< > _CLASS_DECLSPEC
Matrix norm_tools::vm_norm< norm_tools::l2 >(const Matrix& v_from, const Matrix& m_to) {
	Matrix norm(1, m_to.row_num());
	for(ulong i = 0; i < norm.size(); ++i)
		norm[i] = (v_from - m_to.GetRows(i)).norm2();
	return norm;

//		Matrix res = vm_norm2(v_from, m_to);
//		transform(res, ptr_fun< double, double >(std::sqrt));
//		return res;
}
Пример #18
0
	int ExtractCode( Image < uint8 > & DATANOR, Code < float > & codedata, Matrix < float > & Feat,
		 struct osiris_parameter & param, Filtres < float > & Gabor, Appoints & points )
		 {

		   Feat = Featextract( DATANOR, Gabor, points );
		   codedata.SetSize( Feat.GetRows(), Feat.GetCols() );
		   codedata.TMPLATE = TMextract( Feat );
		   codedata.MASK.init( 1 );

		   return ( 0 );
	}
Пример #19
0
//template< norm_types nt >
Matrix::indMatrix norm_tools::sort_distm2(Matrix& dist) {
	Matrix::indMatrix ind;
	ind.reserve(dist.row_num() * (dist.row_num() - 1));

	Matrix drow;
	for(ulong i = 0; i < dist.row_num(); ++i) {
		drow <<= dist.GetRows(i);
		ind &= drow.RawSort();
		dist.SetRows(drow, i);
	}
	//remove first zero column
	dist.DelColumns(0);
	ind.DelColumns(0);
	return ind;
}
Пример #20
0
void ShowMatrix(ostream& os, const Matrix<T>& m)
{
  const size_t MaxR = m.GetRows();
  const size_t MaxC = m.GetCols();
  size_t r, c;

  for (r = 0; r < MaxR; ++r)
  {
    os << "[ ";

    for (c = 0; c < MaxC; ++c)
      os << setw(3) << m.Get(r,c) << " ";

    os << "]\r\n";
  }
}
Пример #21
0
bool IfFilterFactory::toBoolean(const Value& value)
{
	bool result = false;

	wchar_t type = value.GetType();
	switch (type)
	{
	case 'i':
		{
			int i = value.GetInteger();
			result = (i != 0);
		}
		break;
	case 'f':
		{
			double f = value.GetFloat();
			result = (f != 0.0 && f == f);
		}
		break;
	case 'b':
		{
			result = value.GetBool();
		}
		break;
	case 's':
		{
			wstring s = value.GetString();
			result = s.length() > 0 && s != L"false" && s != L"0";
		}
		break;
	case 'm':
		{
			Matrix<Value> m = value.GetArray();
			result = m.GetRows() > 0 && m.GetCols() > 0;
		}
		break;
	default:
		result = false;
		break;
	}

	return result;
}
Пример #22
0
// / Converts a matrix composed of doubles into a FITS file
void mat2fits(Matrix < double >&m, const char *filename)
{
    int status = 0;

    fitsfile *fptr;

    long fpixel = 1, naxis = 2, nelements;

    long naxes[2];

    // Initialise storage
    naxes[0] = (long)m.GetRows();
    naxes[1] = (long)m.GetCols();
    nelements = naxes[0] * naxes[1];

    double *ptrimg;

    // Create pointer image
    ptrimg = (double *)malloc(nelements * sizeof(double));

    for (int ii = 0; ii < naxes[0]; ii++)
        for (int jj = 0; jj < naxes[1]; jj++)
            ptrimg[ ii + jj * naxes[0] ] = m[ii][jj];

    // Create new file, write image, then close file
    if (status == 0)
        fits_create_file(&fptr, filename, &status);
    if (status == 0)
        fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status);
    if (status == 0)
        fits_write_img(fptr, TDOUBLE, fpixel, nelements, &ptrimg[0], &status);
    if (status == 0)
        fits_close_file(fptr, &status);
    free(ptrimg);

    fits_report_error(stderr, status);

}
Пример #23
0
/*
 * returns the determinant of Matrix a
 */
double Det(const Matrix& a)
{
  double d = 0;    // value of the determinant
  int rows = a.GetRows();
  int cols = a.GetRows();

  if (rows == cols)
  {
    // this is a square matrix
    if (rows == 1)
    {
      // this is a 1 x 1 matrix
      d = a.get(1, 1);
    }
    else if (rows == 2)
    {
      // this is a 2 x 2 matrix
      // the determinant of [a11,a12;a21,a22] is det = a11*a22-a21*a12
      d = a.get(1, 1) * a.get(2, 2) - a.get(2, 1) * a.get(1, 2);
    }
    else
    {
      // this is a matrix of 3 x 3 or larger
      for (int c = 1; c <= cols; c++)
      {
        Matrix M = a.Minor(1, c);
        //d += pow(-1, 1+c) * a(1, c) * Det(M);
        d += (c%2 + c%2 - 1) * a.get(1, c) * Det(M); // faster than with pow()
      }
    }
  }
  else
  {
    throw Exception("Matrix must be square");
  }
  return d;
}
Пример #24
0
Matrix<char, float> Matrix<char, float>::operator*(const Matrix<char, float> & p_matrix) const
{
	Matrix<char, float> matrix;

	std::vector<char> rows = GetRows(), columns = GetColumns();

	std::vector<char> _rows = p_matrix.GetRows(), _columns = p_matrix.GetColumns();

	for(int i = 0; i < rows.size(); ++i)
	{
		for(int j = 0; j < _columns.size(); ++j)
		{
			matrix.m_values[Coord(rows[i], _columns[j])] = 0;

			for(int k = 0; k < columns.size(); ++k)
			{
				matrix.m_values[Coord(rows[i], _columns[j])] += GetValue(rows[i], columns[k])*p_matrix.GetValue(_rows[k], _columns[j]);
			}

		}
	}

	return matrix;
}
Пример #25
0
void nn_addon::_fillup_storage(Matrix& p, Matrix& s)
{
	//collect unique samples for learning
	Matrix row;
	double min_dist, cur_dist;
	bool add_chrom;
	for(ulong i = p.row_num() - 1; i < p.row_num(); --i) {
		//clear samples "not in range"
		if(s[i] >= ERR_VAL || (opt_.is_ffRestricted && (s[i] > opt_.sup_ff || s[i] < opt_.inf_ff))) {
			s.DelRows(i);
			p.DelRows(i);
			continue;
		}

		add_chrom = false;
		row <<= p.GetRows(i);
		if(learn_.row_num() == 0)
			add_chrom = true;
		else if(learn_.RRowInd(row) >= learn_.row_num()) {
			//find minimun distance
			min_dist = sqrt((learn_.GetRows(0) - row).norm2());
			for(ulong j = 1; j < learn_.row_num(); ++j) {
				cur_dist = sqrt((learn_.GetRows(j) - row).norm2());
				if(cur_dist < min_dist) min_dist = cur_dist;
			}
			//if distance > tolerance - add to learn samples
			if(min_dist > opt_.tol) add_chrom = true;
		}

		if(add_chrom) {
			learn_ &= row;
			tar_ &= s.GetRows(i);
		}
	}

	//identify best individual
	if(tar_.row_num() > 0) {
		state_.ind_best = tar_.ElementInd(tar_.Min());
		state_.tar_mean = tar_.Mean();
		state_.tar_std = tar_.Std();
	}
	else
		throw ga_except("Empty learning data storage! Nothing to do");

	/*
	if(tar.size() ==0 || abs(_state.tar_std) < 0.000001) {
		//very low variance in data - skip all stuff
		throw ga_except("Very low variance in learning data! Nothing to do");
	}
	*/

#ifdef VERBOSE
	DumpMatrix(learn_, "p.txt");
	DumpMatrix(tar_, "s.txt");
#endif

	//remove very big samples
	//for(long i = _tar.size() - 1; i>=0; --i) {
	//	if(_tar[i] > m + q) {
	//		_tar.DelRows(i);
	//		_learn.DelRows(i);
	//	}
	//
}
Пример #26
0
// Function to interpolate a 2d rectangular grid onto another 2d
// rectangular grid with arbitrary scaling, translation and
// rotation. Uses bilinear interpolation and uses periodic boundary
// conditions i.e. wraps the boundaries in the same way data from an
// FFT is wrapped.
// Taken from David Buscher and converted for use with this Matrix class by
// Fabien Baron
void Interp2d(Matrix < double >&dataIn, Matrix < double >&dataOut,
              double offsetX, double offsetY, double scale, double rotation)
{
    // Matrix<double> &dataIn = Input grid of data points from which to
    // extract a subgrid. The grid pitch is assumed to be the same
    // in both dimensions (this restriction only holds if a non-zero rotation
    // is required)
    // Matrix<double> &dataOut = Output gridded values.
    // double offsetX = X offset of (0,0) point in output grid in input grid
    // coordinate units.
    // If greater than nxIn, the coordinates are wrapped in the X dimension.
    // double offsetY = Y offset of (0,0) point in output grid in input grid
    // coordinate units.
    // If greater than nyIn, the coordinates are wrapped in the Y dimension
    // double scale = output grid pitch measured in units of the input grid
    // pitch
    // double rotation = rotation between input and output grids in radians.
    // Positive values rotate the output axes *clockwise* wrt the input axes

    int ix0, ix1, iy0, iy1;

    double x, y, intX0, intX1, intY0, intY1, fracX, fracY;

    int nxIn = dataIn.GetRows();

    int nyIn = dataIn.GetCols();

    int nxOut = dataOut.GetRows();

    int nyOut = dataOut.GetCols();

    double c = scale * cos(rotation);

    double s = scale * sin(rotation);

    for (int iy = 0; iy < nyOut; iy++)
    {
        for (int ix = 0; ix < nxOut; ix++)
        {
            x = c * ix + s * iy + offsetX;
            y = -s * ix + c * iy + offsetY;

            /* Get integer and fractional parts of x position */
            intX0 = floor(x);
            intX1 = intX0 + 1.0;
            fracX = x - intX0;

            /* Do modulo arithmetic correctly for negative numbers */
            ix0 = (int)(intX0 - nxIn * floor(intX0 / nxIn));
            ix1 = (int)(intX1 - nxIn * floor(intX1 / nxIn));

            /* Repeat for y */
            intY0 = floor(y);
            intY1 = intY0 + 1.0;
            fracY = y - intY0;
            iy0 = (int)(intY0 - nyIn * floor(intY0 / nyIn));
            iy1 = (int)(intY1 - nyIn * floor(intY1 / nyIn));

            dataOut[ix][iy] = fracX * (fracY * dataIn[ix1][iy1] 
                           + (1.0 - fracY) * dataIn[ix1][iy0]) 
                           + (1.0 - fracX) * (fracY * dataIn[ix0][iy1] +
                                        (1.0 - fracY) * dataIn[ix0][iy0]);
        }
    }

}
Пример #27
0
/*
 * returns the inverse of Matrix a
 */
Matrix Inv(const Matrix& a)
{
  Matrix res;
  double d = 0;    // value of the determinant
  int rows = a.GetRows();
  int cols = a.GetRows();

  d = Det(a);
  if (rows == cols && d != 0)
  {
    // this is a square matrix
    if (rows == 1)
    {
      // this is a 1 x 1 matrix
      res = Matrix(rows, cols);
      res(1, 1) = 1 / a.get(1, 1);
    }
    else if (rows == 2)
    {
      // this is a 2 x 2 matrix
      res = Matrix(rows, cols);
      res(1, 1) = a.get(2, 2);
      res(1, 2) = -a.get(1, 2);
      res(2, 1) = -a.get(2, 1);
      res(2, 2) = a.get(1, 1);
      res = (1/d) * res;
    }
    else
    {
      // this is a matrix of 3 x 3 or larger
      // calculate inverse using gauss-jordan elimination
      //   http://mathworld.wolfram.com/MatrixInverse.html
      //   http://math.uww.edu/~mcfarlat/inverse.htm
      res = Diag(rows);   // a diagonal matrix with ones at the diagonal
      Matrix ai = a;    // make a copy of Matrix a

      for (int c = 1; c <= cols; c++)
      {
        // element (c, c) should be non zero. if not, swap content
        // of lower rows
        int r;
        for (r = c; r <= rows && ai(r, c) == 0; r++)
        {
        }
        if (r != c)
        {
          // swap rows
          for (int s = 1; s <= cols; s++)
          {
            Swap(ai(c, s), ai(r, s));
            Swap(res(c, s), res(r, s));
          }
        }

        // eliminate non-zero values on the other rows at column c
        for (int r = 1; r <= rows; r++)
        {
          if(r != c)
          {
            // eleminate value at column c and row r
            if (ai(r, c) != 0)
            {
              double f = - ai(r, c) / ai(c, c);

              // add (f * row c) to row r to eleminate the value
              // at column c
              for (int s = 1; s <= cols; s++)
              {
                ai(r, s) += f * ai(c, s);
                res(r, s) += f * res(c, s);
              }
            }
          }
          else
          {
            // make value at (c, c) one,
            // divide each value on row r with the value at ai(c,c)
            double f = ai(c, c);
            for (int s = 1; s <= cols; s++)
            {
              ai(r, s) /= f;
              res(r, s) /= f;
            }
          }
        }
      }
    }
  }
  else
  {
    if (rows == cols)
    {
      throw Exception("Matrix must be square");
    }
    else
    {
      throw Exception("Determinant of matrix is zero");
    }
  }
  return res;
}
Пример #28
0
// / Converts a matrix composed of complex numbers into a FITS file
void mat2fits(Matrix < Complex > &m, char *filename)  // TBD: fix row major vs colum major
{
    int status = 0;

    fitsfile *fptr;

    long fpixel = 1, naxis = 2, nelements;

    long naxes[2];

    // Initialise storage
    naxes[0] = (long)m.GetRows();
    naxes[1] = (long)m.GetCols();
    nelements = naxes[0] * naxes[1];

    // Filenames
    char fitsreal[100];

    char fitsimag[100];

    for (int i = 0; i < 100; i++)
    {
        fitsreal[i] = '\0';
        fitsimag[i] = '\0';
    }

    strcpy(fitsreal, filename);
    strcpy(fitsimag, filename);
    strcat(fitsreal, "_r");
    strcat(fitsimag, "_i");

    double *ptrimgreal = (double *)malloc(nelements * sizeof(double));

    for (int ii = 0; ii < naxes[0]; ii++)
        for (int jj = 0; jj < naxes[1]; jj++)
            ptrimgreal[ii + jj * naxes[0]] = m[naxes[0] - ii - 1][jj].real();

    // Create new file, write image, then close file
    if (status == 0)
        fits_create_file(&fptr, fitsreal, &status);
    if (status == 0)
        fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status);
    if (status == 0)
        fits_write_img(fptr, TDOUBLE, fpixel, nelements, &ptrimgreal[0],
                       &status);
    if (status == 0)
        fits_close_file(fptr, &status);

    free(ptrimgreal);

    double *ptrimgimag = (double *)malloc(nelements * sizeof(double));

    for (int ii = 0; ii < naxes[0]; ii++)
        for (int jj = 0; jj < naxes[1]; jj++)
            ptrimgimag[ii + jj * naxes[0]] = m[ii][jj].imag();

    // Create new file, write image, then close file
    if (status == 0)
        fits_create_file(&fptr, fitsimag, &status);
    if (status == 0)
        fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status);
    if (status == 0)
        fits_write_img(fptr, TDOUBLE, fpixel, nelements, &ptrimgimag[0],
                       &status);
    if (status == 0)
        fits_close_file(fptr, &status);

    free(ptrimgimag);

    fits_report_error(stderr, status);

}
Пример #29
0
Matrix nn_addon::_kmeans_filter(clusterizer& cengine, const Matrix& p, const Matrix& f, Matrix& lp, Matrix& lf)
{
	//select which function to call depending on clustering engine
	struct find_clusters {
		static void go(KM::kmeans& cengine, const Matrix& p, const Matrix& f, double mult, ulong maxiter) {
			cengine.find_clusters_f(p, f, mult, maxiter, NULL, false);
			//cengine.drops_hetero_map(p, f, mult, maxiter);
			//cengine.drops_hetero_simple(p, f, mult, 200);
		}
		static void go(DA::determ_annealing& cengine, const Matrix& p, const Matrix& f, double mult, ulong maxiter) {
			cengine.find_clusters(p, f, mult, maxiter);
		}
	};

	//do a clusterization of learning data
	find_clusters::go(cengine, p, f, max< ulong >(p.row_num() * opt_.kmf_cmult, 1), 200);

	//cengine.drops_hetero_simple(p, f, opt_.kmf_cmult, 200);
	//cengine.find_clusters_f(p, f, max< ulong >(p.row_num() * opt_.kmf_cmult, 1), 200, NULL, false);

	//cengine.find_clusters(p, max<ulong>(p.row_num() * opt_.kmf_cmult, 1), 200, false, NULL, true);
	//cengine.opt_.use_prev_cent = true;

	Matrix c = cengine.get_centers();
	ulMatrix ind = cengine.get_ind();

	//find best solution
	ulong best_ind = f.min_ind();
	Matrix cur_b = p.GetRows(best_ind);
#ifdef VERBOSE
	DumpMatrix(cur_b | f.GetRows(best_ind), "best_sol.txt");
#endif

	//calc distances from best solution to all cluster centers
	Matrix dist(1, c.row_num());
	for(ulong i = 0; i < c.row_num(); ++i)
		dist[i] = (c.GetRows(i) - cur_b).norm2();

	//sort distances
	ulMatrix ci = dist.RawSort();

	//get affiliation
	const KM::kmeans::vvul& aff = cengine.get_aff();
	//collect learning samples, & search for closest centers and more
	lp.clear();	lf.clear();
	lp.reserve(opt_.bestCount*p.col_num()); lf.reserve(opt_.bestCount*f.row_num());
	ulong search_bound = 0;
	Matrix close_cl;
	bool learn_built = false, search_built = false;
	ulong c_ind = 0;
	for(; c_ind < c.row_num() && !learn_built; ++c_ind) {
		if(opt_.learn_clust_num > 0 && c_ind >= opt_.learn_clust_num) break;
		if(opt_.search_clust_num > 0 && c_ind >= opt_.search_clust_num) search_built = true;
		close_cl &= c.GetRows(ci[c_ind]);
		const ulong* cur_aff = (ulong*)&aff[ci[c_ind]][0];
		for(ulong j = 0; j < aff[ci[c_ind]].size(); ++j) {
			lp &= p.GetRows(cur_aff[j]); lf &= f.GetRows(cur_aff[j]);
			if(lp.row_num() >= (ulong)opt_.bestCount) learn_built = true;
			if(!search_built) ++search_bound;
		}
		/*
		for(ulong j = 0; j < ind.size() && !learn_built; ++j) {
			if(ind[j] == ci[c_ind]) {
				lp &= p.GetRows(j); lf &= f.GetRows(j);
				if(lp.row_num() >= opt_.bestCount) learn_built = true;
				if(!search_built) ++search_bound;
			}
		}
		*/
	}
	//update closest centers - add centers within learning points square
	Matrix lpmm = lp.minmax();
	Matrix lpmin = lpmm.GetRows(0), lpmax = lpmm.GetRows(1);
	Matrix cur_c;
	state_.learn_cl_cent.clear();
	for(; c_ind < c.row_num(); ++c_ind) {
		cur_c <<= c.GetRows(ci[c_ind]);
		if(cur_c >= lpmin && cur_c <= lpmax)
			close_cl &= cur_c;
		else
			state_.learn_cl_cent &= cur_c;
	}
	//insert closest cluster centers in the beginning
	state_.learn_cl_cent <<= close_cl & state_.learn_cl_cent;

#ifdef VERBOSE
	DumpMatrix(close_cl, "c.txt");
#endif

	if(opt_.search_clust_num == 0 && opt_.search_samples_num > 0)
		search_bound = opt_.search_samples_num;

	if(search_bound < lp.row_num()) {
#ifdef VERBOSE
		DumpMatrix(lp.GetRows(0, search_bound), "ss.txt");
#endif
		return lp.GetRows(0, search_bound).minmax();
	}
	else {
#ifdef VERBOSE
		DumpMatrix(lp, "ss.txt");
#endif
		return lp.minmax();
	}
}
Пример #30
0
void FFT(Matrix < Complex > &min, Matrix < Complex > &mout, int direction, int dccenter)
{
    assert(min.GetCols() == min.GetRows());     // square matrix
    assert(mout.GetCols() == mout.GetRows());
    assert(mout.GetCols() == min.GetRows());
    int nfft = min.GetCols();

    fftw_complex *in =
        (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * nfft * nfft);
    fftw_complex *out =
        (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * nfft * nfft);

    for (int iy = 0; iy < nfft; iy++)
    {
        for (int ix = 0; ix < nfft; ix++)
        {
            in[ix + nfft * iy][0] = min[ix][iy].real();
            in[ix + nfft * iy][1] = min[ix][iy].imag();
        }
    }

    fftw_plan p;

    if (direction >= 0)
        p = fftw_plan_dft_2d(nfft, nfft, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    else
        p = fftw_plan_dft_2d(nfft, nfft, in, out, FFTW_BACKWARD,
                             FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
    fftw_free(in);

    if (dccenter == 0)
    {
        for (int iy = 0; iy < nfft; iy++)
            for (int ix = 0; ix < nfft; ix++)
                mout[ix][iy]
                    = Complex(out[ix + nfft * iy][0], out[ix + nfft * iy][1])
                    / double (nfft);
    }
    else
    {
        for (int iy = 0; iy < nfft / 2; iy++)
        {
            for (int ix = 0; ix < nfft / 2; ix++)
            {
                mout[ix][iy] =
                    Complex(out[nfft / 2 + ix + nfft * (iy + nfft / 2)][0],
                            out[nfft / 2 + ix +
                                nfft * (iy + nfft / 2)][1]) / double (nfft);
            }

            for (int ix = nfft / 2; ix < nfft; ix++)
            {
                mout[ix][iy] =
                    Complex(out[ix - nfft / 2 + nfft * (iy + nfft / 2)][0],
                            out[ix - nfft / 2 +
                                nfft * (iy + nfft / 2)][1]) / double (nfft);
            }
        }

        for (int iy = nfft / 2; iy < nfft; iy++)
        {
            for (int ix = 0; ix < nfft / 2; ix++)
            {
                mout[ix][iy] =
                    Complex(out[ix + nfft / 2 + nfft * (iy - nfft / 2)][0],
                            out[ix + nfft / 2 +
                                nfft * (iy - nfft / 2)][1]) / double (nfft);
            }

            for (int ix = nfft / 2; ix < nfft; ix++)
            {
                mout[ix][iy] =
                    Complex(out[ix - nfft / 2 + nfft * (iy - nfft / 2)][0],
                            out[ix - nfft / 2 +
                                nfft * (iy - nfft / 2)][1]) / double (nfft);
            }
        }
    }
    fftw_free(out);

}