void Gaussian<T>::generate(VectorT& x) const { //generate a normalized gaussian distributed variable y VectorT y(numDims()); for(int i=0;i<y.n;i++) y(i) = RandGaussian(0,1); L.mul(y,x); x += mu; }
T Gaussian<T>::probability(const VectorT& x) const { int d = numDims(); T det=One; for(int i=0;i<d;i++) det*=L(i,i); T invc = Pow(2.0*Pi,0.5*T(d))*det; VectorT x_mu,y; x_mu.sub(x,mu); y.resize(L.n); LBackSubstitute(L,x_mu,y); return Exp(-Half*y.normSquared())/invc; }
const array::array_proxy array::operator()(const index &s0) const { index z = index(0); if(isvector()){ switch(numDims(this->arr)) { case 1: return gen_indexing(*this, s0, z, z, z); case 2: return gen_indexing(*this, z, s0, z, z); case 3: return gen_indexing(*this, z, z, s0, z); case 4: return gen_indexing(*this, z, z, z, s0); default: AF_THROW_ERR("ndims for Array is invalid", AF_ERR_SIZE); } } else { return gen_indexing(*this, s0, z, z, z, true); } }
// Assign values to an array array::array_proxy& af::array::array_proxy::operator=(const array &other) { unsigned nd = numDims(impl->parent_->get()); const dim4 this_dims = getDims(impl->parent_->get()); const dim4 other_dims = other.dims(); int dim = gforDim(impl->indices_); af_array other_arr = other.get(); bool batch_assign = false; bool is_reordered = false; if (dim >= 0) { //FIXME: Figure out a faster, cleaner way to do this dim4 out_dims = seqToDims(impl->indices_, this_dims, false); batch_assign = true; for (int i = 0; i < AF_MAX_DIMS; i++) { if (this->impl->indices_[i].isBatch) batch_assign &= (other_dims[i] == 1); else batch_assign &= (other_dims[i] == out_dims[i]); } if (batch_assign) { af_array out; AF_THROW(af_tile(&out, other_arr, out_dims[0] / other_dims[0], out_dims[1] / other_dims[1], out_dims[2] / other_dims[2], out_dims[3] / other_dims[3])); other_arr = out; } else if (out_dims != other_dims) { // HACK: This is a quick check to see if other has been reordered inside gfor // TODO: Figure out if this breaks and implement a cleaner method other_arr = gforReorder(other_arr, dim); is_reordered = true; } } af_array par_arr = 0; if (impl->is_linear_) { AF_THROW(af_flat(&par_arr, impl->parent_->get())); nd = 1; } else { par_arr = impl->parent_->get(); } af_array tmp = 0; AF_THROW(af_assign_gen(&tmp, par_arr, nd, impl->indices_, other_arr)); af_array res = 0; if (impl->is_linear_) { AF_THROW(af_moddims(&res, tmp, this_dims.ndims(), this_dims.get())); AF_THROW(af_release_array(par_arr)); AF_THROW(af_release_array(tmp)); } else { res = tmp; } impl->parent_->set(res); if (dim >= 0 && (is_reordered || batch_assign)) { if (other_arr) AF_THROW(af_release_array(other_arr)); } return *this; }
unsigned array::numdims() const { return numDims(get()); }