void apply(cube<real>& v, real bias) noexcept override
 {
     real* d = v.data();
     size_t  n = v.num_elements();
     for ( size_t i = 0; i < n; ++i )
         d[i] = f_(d[i] + bias);
 }
Exemple #2
0
//HELIX ANIMATION 
uint8_t animation::helix(cube &c){
    float X = 0;
    float Y = 0;
    float Z = 0;
    
    slow++;
    if(slow < 100){
        return 0;
    }
    slow = 0;
    
    c.clearVoxels();
    
    //use my fancy pants sine function
    for(uint8_t i = 0; i < 3; i++){
        for(uint8_t z = 0; z < 4; z++){
            Z = z*52;
            X = get_sinA(Z + phase + 18*i);
            Y = get_sinA(Z + phase + 90 + 18*i);
            X = (X+1)*4;
            Y = (Y+1)*4;
            c.setVoxel((uint8_t)X, (uint8_t)Y, z);
            c.setVoxel((uint8_t)(8-X), (uint8_t)(8-Y), z+4);
        }
    }
    
    //increment the phase 
    phase+=18;
    
    if(phase > 360){
        phase -= 360;
    }
    
    return 1;
}
Exemple #3
0
cube barrel_distort_rgb(const cube &F, double offset_x) {
  cube G(F.n_rows, F.n_cols, F.n_slices);
  G.slice(0) = barrel_distort(F.slice(0), offset_x);
  G.slice(1) = barrel_distort(F.slice(1), offset_x);
  G.slice(2) = barrel_distort(F.slice(2), offset_x);
  return G;
}
Exemple #4
0
SEXP move_B(const mat& y, cube& B, const mat& kappa_star, const field<mat>& C,
            //const field<sp_mat>& C,
            mat& gamma, const mat& D, const ucolvec& s, double tau_e)
{
     BEGIN_RCPP
     // N x T matrix of standardized counts, y
     // B = (B_1,...,B_K), where B_k is N x T matrix for iGMRF term k
     // N x T matrix, gamma = sum_{k=1^K}(B_k)
     // K x T, D, where row k contains T x 1 diagonal elements of Q_k
     // sample cluster assignments, s(1), ..., s(N)  
     // Q = (Q_1,...,Q_K), where Q_k is a T x T de-scaled iGMRF precision matrix
     // C = (C_1,...,C_K), where C_k = D_k^-1 * Omega_k, 
     // where Omega_k is the T x T adjacency matrix for iGMRF term, k
     // D is a K x T matrix where row k contains T diagonal elements of Q_k
     // K x M matrix, kappa_star records locations for each iGMRF term 
     int K     = B.n_slices;
     int N     = y.n_rows;
     int T     = D.n_cols;
     colvec bbar_ki(T); bbar_ki.zeros();
     rowvec gammatilde_ki(T); gammatilde_ki.zeros();
     rowvec ytilde_ki(T); ytilde_ki.zeros();
     rowvec d_k(T);
     vec zro(T); zro.zeros();
     double e_ij, phi_ij, bkij, h_ij;
     int k = 0, i = 0, j = 0; /* loop variables */
     for( k = 0; k < K; k++ ) /* over iGMRF terms */
     {
          d_k                           = D.row(k);
          for( i = 0; i < N; i++ ) /* over units */
          {
               /* take out T x 1, b_ki, to be sampled from gamma */
               //gammatilde_ki        = gamma.row(i) - B.slice(k).row(i); /* 1 x T */
               //ytilde_ki            = y.row(i) - gammatilde_ki;
               gammatilde_ki            = gamma.row(i); /* 1 x T */
               // mean of univariate iGMRF, b_kij = 1/d_kj * (omega_kj(-j) * b_ki(-j))
               bbar_ki                  = C(k,0) * B.slice(k).row(i).t(); /* T x 1 */
               for( j = 0; j < T; j++ ) /* over time points */
               {
                    gammatilde_ki(j)    -= B.slice(k)(i,j);
                    ytilde_ki(j)        = y(i,j) - gammatilde_ki(j);
                    // mean of univariate iGMRF, b_kij = 1/d_kj * (omega_kj(-j) * b_ki(-j))
                    B.slice(k)(i,j)     = 0;
                    //bbar_ki(j)          = dot( C(k,0).row(j), B.slice(k).row(i) );
                    e_ij                = tau_e*ytilde_ki(j) 
                                             + d_k(j)*kappa_star(k,s(i)) * bbar_ki(j);
                    phi_ij              = tau_e + d_k(j)*kappa_star(k,s(i));
                    h_ij                = (e_ij / phi_ij);
                    bkij                = rnorm( 1, (h_ij), sqrt(1/phi_ij) )[0];
                    B.slice(k)(i,j)     = bkij;
                    /* put back new sampled values for b_kij */
                    gammatilde_ki(j)    += B.slice(k)(i,j);
               } /* end loop j over time points */
               /* put back new sampled values for b_ki */
               //gamma.row(i)         = gammatilde_ki + B.slice(k).row(i); 
               gamma.row(i)        = gammatilde_ki;
          } /* end loop i over units */
     } /* end loop k over iGMRF terms */
     END_RCPP
} /* end function ustep to sample B_1,...,B_K */
Exemple #5
0
inline bool equal(const cube<T>& c1, const cube<T>& c2)
{
    if ( c1.n_elem != c2.n_elem )
    {
        return false;
    }

    return std::equal(c1.memptr(), c1.memptr() + c1.n_elem, c2.memptr());
}
 typename std::enable_if<has_public_member_grad<T>::value>::type
 apply_grad(cube<real>& g, const cube<real>& f, const T& fn) noexcept
 {
     real* gp = g.data();
     const real* fp = f.data();
     size_t  n = g.num_elements();
     for ( size_t i = 0; i < n; ++i )
         gp[i] *= fn.grad(fp[i]);
 }
Exemple #7
0
inline void pairwise_div( cube<T>& a, const cube<T>& b )
{
    ZI_ASSERT(a.n_elem==b.n_elem);
    T* ap = a.memptr();
    const T* bp = b.memptr();

    for ( size_t i = 0; i < a.n_elem; ++i )
        ap[i] /= bp[i];
}
inline void convolve_constant( cube<T> const & a,
                               identity_t<T> b,
                               cube<T> & r) noexcept
{
    ZI_ASSERT(size(a)==size(r));

    T const * ap = a.data();
    T * rp = r.data();

    for ( size_t i = 0; i < r.num_elements(); ++i )
        rp[i] = ap[i] * b;
}
// REALIZATION OF FUNCTIONS
void rotationMain(int x) {
    if (x<=90) {
        glutTimerFunc(5,rotationMain,x+1);
        animating = true;
        Cube.rotateCubesAnim(direction,x);
        glutPostRedisplay();
    } else {
        Cube.rotateCubesAnim(direction,0);
        Cube.rotateCubesFace(direction);
        animating = false;
        glutPostRedisplay();
    }
}
inline T convolve_constant_flipped( cube<T> const & a,
                                    cube<T> const & b ) noexcept
{
    ZI_ASSERT(size(a)==size(b));

    T r = 0;
    T const * ap = a.data();
    T const * bp = b.data();

    for ( size_t i = 0; i < a.num_elements(); ++i )
        r += ap[i] * bp[i];

    return r;
}
Exemple #11
0
/**********************************************************************
*   MSE Class
***********************************************************************/
double MSE::cost(cube pred, cube y){
    dbg_assert(y.n_cols == 1 && y.n_slices == 1);
    pred.reshape(pred.n_elem,1,1);
    y.reshape(y.n_elem,1,1);
    
    double retn = 0.0f;
    for(int i=0; i < y.n_elem; i++) {
        retn += pow(pred(i,0,0) - y(i,0,0),2);
    }
    retn /= y.n_elem;
    retn *= 0.5f;
    //return(retn);
    return(0.5 * mean(mean(square(pred.slice(0) - y.slice(0)))));        
}
Exemple #12
0
//RANDOM LIGHTS
uint8_t animation::randomExpand(cube &c){
    uint16_t count = 0;
	uint8_t rX, rY, rZ;
    
    slow++;
    if(slow < 20){
        return 0;
    }
    slow = 0;
    
    randomSeed(analogRead(0));
    rX = rand()%8+0;
    rY = rand()%8+0;
    rZ = rand()%8+0;
	
    //find an empty voxel
    while(c.getVoxel(rX, rY, rZ) == 1 && dir == 1){
        rX = random(0, 8);
        rY = random(0, 8);
        rZ = random(0, 8);
        count++;
        
        if(count > 200){
            dir *= -1;
        }
    }
    count = 0;

    //find a full voxel
    while(c.getVoxel(rX, rY, rZ) == 0 && dir == -1){
        rX = random(0, 8);
        rY = random(0, 8);
        rZ = random(0, 8);
        count++;

        if(count > 200){
            dir *= -1;
        }
    }

    //fill or clear the voxel found
    if(dir == 1){
        c.setVoxel(rX, rY, rZ);
    }else{
        c.clearVoxel(rX, rY, rZ);
    }
    
    return 1;
}
Exemple #13
0
cube Utils::conv3(cube body, cube kernel)
{
	if (body.max() <= 0)
	{
		return zeros(body.n_rows, body.n_cols, body.n_slices);
	}
	cube Q(body);
	for (int i = 1; i < body.n_slices - 1; i++)
	{
		Q.slice(i) = conv2(body.slice(i), kernel.slice(1), "same") +
			conv2(body.slice(i - 1), kernel.slice(0), "same") +
			conv2(body.slice(i + 1), kernel.slice(2), "same");
	}
	return Q;
}
    // performs inplace dropout backward
    inline void dropout_backward(cube<real> & g)
    {
        ZI_ASSERT(mask_);

        size_t s = g.num_elements();
        for ( size_t i = 0; i < s; ++i )
        {
            if ( mask_->data()[i] )
                g.data()[i] *= scale();
            else
                g.data()[i]  = static_cast<real>(0);
        }

        // Should we reset mask_ here?
    }
Exemple #15
0
//SINGLE PLANE BOUNCING
uint8_t animation::bouncePlane(cube &c, uint8_t axis){
    slow++;
    if(slow < 200){
        return 0;
    }
    slow = 0;
    
    //the X animation looks the same but actually clears everything in its path
    //rather than clear everything at the start, it makes a simple but cool 
    //transition between some animations
    if(axis != 'X'){
        c.clearVoxels();
    }
    
    switch(axis){
        case 'X':
            for(uint8_t z = 0; z < Zd; z++){
                for(uint8_t y = 0; y < Yd; y++){
                    for(uint8_t x = 0; x < Xd; x++){
                        if(x == pos - dir){
                            c.clearVoxel(x, y, z);
                        }
                        c.setVoxel(pos, y, z);
                    }
                }
            }
            break;
        case 'Y':
            for(uint8_t x = 0; x < Xd; x++){
                for(uint8_t z = 0; z < Zd; z++){
                    c.setVoxel(x, pos, z);
                }
            }
            break;
        case 'Z':
            for(uint8_t x = 0; x < Xd; x++){
                for(uint8_t y = 0; y < Yd; y++){
                    c.setVoxel(x, y, pos);
                }
            }
            break;
    }
    
    //bounce the pos variable between 0 and 7
    bouncePos();
    
    return 1;
}   
cube imresize2(const cube &C, uword m, uword n) {
  cube F(m, n, C.n_slices);
  for (uword k = 0; k < C.n_slices; k++) {
    F.slice(k) = imresize2(C.slice(k), m, n);
  }
  return F;
}
cube noncont_slices(const cube & X, const uvec & index, int r_start, int c_start, int r_stop, int c_stop){
	cube Xsub(r_stop-r_start+1, c_stop-c_start+1, index.n_elem);
	for(unsigned int i=0; i<index.n_elem; i++){
		Xsub.slice(i) = X.slice(index[i]).submat(r_start, c_start, r_stop, c_stop);
	}
	return Xsub; 
}
Exemple #18
0
cube Utils::erode(cube body, cube kernel)
{
	int minx, miny, minz, maxx, maxy, maxz;
	Utils::bounds(body, minx, miny, minz, maxx, maxy, maxz);
	cube result = cube(body);
	int size_x = kernel.n_cols / 2;
	int size_y = kernel.n_rows / 2;
	int size_z = kernel.n_slices / 2;
	maxx -= kernel.n_cols;
	maxy -= kernel.n_rows;
	maxz -= kernel.n_slices;
	for (int i = minx; i <= maxx; i++)
	{
		for (int j = miny; j <= maxy; j++)
		{
			for (int k = minz; k <= maxz; k++)
			{
				cube subcube = body.subcube(j, i, k, size(kernel));
				subcube -= kernel;
				result(j + size_y, i + size_x, k + size_z) = subcube.min() < 0 ? 0 : 1;
			}
		}
	}
	return result;
}
cube conv2(const cube &F, const mat &H) {
  cube G(F.n_rows, F.n_cols, F.n_slices);
  for (uword i = 0; i < F.n_slices; i++) {
    G.slice(i) = conv2(F.slice(i), H);
  }
  return G;
}
Exemple #20
0
inline unique_cube<T> crop_right( const cube<T>& in, const vec3s& s )
{
    auto r = pool<T>::get_unique(s);
    vec3s b = size(in) - s;
    *r = in.subcube(b[0], b[1], b[2], b[0]+s[0]-1, b[1]+s[1]-1, b[2]+s[2]-1);
    return r;
}
//! compute (trial)point-to-(model)point Mahalanobis distance
//! @param[in] index		index of the points (in trial and model) to be compared
//! @param[in] &trial		reference to the trial
//! @param[in] &model		reference to the model
//! @param[in] &variance	reference to the model variance
//! @return 			    Mahalanobis distance between trial-point and model-point
float Classifier::mahalanobisDist(int index,mat &trial,mat &model,cube &variance)
{
	mat difference = trial.col(index) - model.col(index);
	mat distance = (difference.t() * (variance.slice(index)).i()) * difference;

	return distance(0,0);
}
Exemple #22
0
// Volumetric 2D Convolution
mat conv2d(cube image, cube kernel){
 
    dbg_assert(image.n_rows == image.n_cols);
    dbg_assert(kernel.n_rows == kernel.n_cols);
    dbg_assert(kernel.n_rows <= image.n_rows);
    dbg_assert(kernel.n_slices == image.n_slices);

    int kernel_size = kernel.n_rows;
    int image_size  = image.n_rows;

    int result_size = image_size - kernel_size + 1;
    mat result      = zeros<mat>(result_size, result_size);

    for(int row=0; row < result_size; row++){
        for(int col=0; col < result_size; col++){
            result(row, col) = accu(
                                 image.tube(
                                    row, 
                                    col, 
                                    row + kernel_size - 1,
                                    col + kernel_size - 1
                                 ) % kernel 
                               );
        }
    }
    
    return(result);
       
}
void initScene() {
    // Clear
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Set drawing mode
    glMatrixMode(GL_MODELVIEW);

    // Draw scene
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,-7.0f);

    // Draw lighting
	GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

    //Add positioned light
	GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
	GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

	//Add directed light
	GLfloat lightColor1[] = {0.7f, 0.7f, 0.7f, 1.0f}; //Color (0.5, 0.2, 0.2)
	//Coming from the direction (-1, 0.5, 0.5)
	GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
	glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
	glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);

    // Draw the cube
    Cube.draw(_textureId);

    glutSwapBuffers();
}
Exemple #24
0
void connected_cube_list::add( const cube& c )
{
  /* check if cube already exists */
  auto it = _connected_cubes.find( c );
  if ( it != _connected_cubes.end() ) return;

  /* find matching cubes */
  cube_common_vec_t commons;
  int weight = 0;
  for ( const auto& other : _cubes )
  {
    int m = other.match_intersect( c );
    if ( m != -1 )
    {
      commons += std::make_pair( other, (unsigned)m );
      weight += c.dimension() - m - 1;
      _connected_cubes[other] += std::make_pair( c, (unsigned)m );
      cube_weights[other] += other.dimension() - m - 1;
    }
  }

  /* add cube */
  _cubes += c;
  _connected_cubes[c] = commons;
  cube_weights[c] = weight;
}
void lmMean(const cube& wX, const rowvec& vec_beta, mat& mean){
	int n = wX.n_slices, P=wX.n_cols;
	mean = zeros(n,P);
	for(int i=0;i<n;i++){
		mean.row(i) = vec_beta* wX.slice(i);
	}
}
cube noncont_slices(const cube & X, const uvec & index){
	cube Xsub(X.n_rows, X.n_cols, index.n_elem);
	for(unsigned int i=0; i<index.n_elem; i++){
		Xsub.slice(i) = X.slice(index[i]);
	}
	return Xsub; 
}
inline cube_p<T> convolve_sparse( cube<T> const & a,
                                  cube<T> const & b,
                                  vec3i const & s )
{
    if ( s == vec3i::one )
    {
        return convolve(a,b);
    }

    vec3i as = size(a);
    vec3i bs = size(b);
    vec3i rs = size(a) - (size(b) - vec3i::one) * s;

    cube_p<T> r = get_cube<T>(rs);

    int a_strides[3]  = { s[2], as[2]*s[1], as[2]*as[1]*s[0] };
    int r_strides[3]  = { s[2], rs[2]*s[1], rs[2]*rs[1]*s[0] };

    // sparseness
    for (int xs=0; xs<s[0]; xs++)
        for (int ys=0; ys<s[1]; ys++)
            for (int zs=0; zs<s[2]; zs++)
            {
                vec3i in_size( (as[0]-1)/s[0] + (xs == 0 ? 1 : 0),
                               (as[1]-1)/s[1] + (ys == 0 ? 1 : 0),
                               (as[2]-1)/s[2] + (zs == 0 ? 1 : 0) );

                const T* in_ptr  = &(a[xs][ys][zs]);
                T* out_ptr = &((*r)[xs][ys][zs]);

#ifdef ZNN_USE_FLOATS
                int status = vslsConvExec( conv_plans.get(in_size, bs),
                                           in_ptr, a_strides,
                                           b.data(), NULL,
                                           out_ptr, r_strides);
#else
                int status = vsldConvExec( conv_plans.get(in_size, bs),
                                           in_ptr, a_strides,
                                           b.data(), NULL,
                                           out_ptr, r_strides);
#endif

            }

    return r;
}
Exemple #28
0
inline void fill_indices(cube<T>& c)
{
    T  idx = 0;
    T* mem = c.memptr();

    for ( size_t i = 0; i < c.n_elem; ++i, ++idx )
        mem[i] = idx;
}
Exemple #29
0
sseq
compute_szabo_sseq (const cube<Z2> &c)
{
  mod_map<Z2> d = c.compute_d (0, 0, 0, 0, 0);
  
  sseq_builder builder (c.khC, d);
  return builder.build_sseq ();
}
void white(const mat& y, const mat& y_center, const uvec& sti, const cube& matphi1, const cube& matphi2, mat& wy, mat& wy_center, const uvec& WTIME){
	int L=matphi1.n_slices, wT = WTIME.n_elem, t=0; 
	mat multiply;
	if(L==0){wy=y; wy_center = y_center;}
	else{
		wy = y.rows(WTIME);
		wy_center = y_center.rows(WTIME);
		for(int wt=0; wt<wT ; wt++){
			t = WTIME[wt];
			for(int l=0; l<L; l++){
				multiply = (sti[t-l-1])?(matphi1.slice(l)):(matphi2.slice(l));
				wy.row(wt) -= y.row(t-l-1)*multiply;					
				wy_center.row(wt) -= y_center.row(t-l-1)*multiply;
			}
		}
	}
}