예제 #1
0
vector<Mat> SegmentDataByIDX(const Mat& data, const Mat& idx)
{
    assert(data.type() == CV_32F || data.type() == CV_64F);
    assert(data.rows > 0 && data.cols > 0 && idx.rows > 0 && idx.cols > 0);
    assert(data.cols == idx.cols);
    Array2D idx_hash;
    if (idx.type() != CV_32S) {
        Mat idx_tmp = idx;
        Mat_<int> tmp;
        idx_tmp.convertTo(tmp, CV_32S);
        idx_hash = ComputeIDX(tmp);
    } else{
        idx_hash = ComputeIDX(idx);
    }
    if (idx_hash.empty()) { return vector<Mat>(); }
    vector<Mat> result(idx_hash.size());
    for (int i = 0; i < idx_hash.size(); i++) {
        Array1D cur = idx_hash[i];
        if (cur.empty()) { return vector<Mat>(); }
        Mat temp(data.rows, cur.size(), data.type());
        Mat col(data.rows, 1, data.type());
        for (int j = 0; j < cur.size(); j++) {
            col = temp(Range(0, data.rows), Range(j, j + 1));
            data(Range(0, data.rows), Range(cur[j] - 1, cur[j])).copyTo(col);
        }
        result[i] = temp;
    }
    return result;
}
예제 #2
0
BayesNetwork::BayesNetwork(Array1D<Discretizer*> &discretizers)
  : parentIndex(discretizers.size()),
    probTables(discretizers.size()),
    discretizers(discretizers)
{
  parentIndex.setAllTo(-1);
}
예제 #3
0
Array2D CombinationWithDuplicatedElementsByNonRecursion(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }

    std::sort(array.begin(), array.end());
    result.push_back(Array1D());
    int last = array[0], opt_result_num = 1;
    for (int i = 0; i < array.size(); i++) {
        if (array[i] != last) {
            last = array[i];
            opt_result_num = result.size();
        }
        Array2D::size_type result_size = result.size();
        for (int j = result_size - 1; j >= result_size - opt_result_num; j--) {
            result.push_back(result[j]);
            result.back().push_back(array[i]);
        }
    }
    return result;
}
예제 #4
0
파일: gl_2_0.cpp 프로젝트: gatgui/pygl
static PyObject* py_glDrawBuffers(PyObject *, PyObject *args) {
  CHECK_ARG_COUNT(args, 1);
  Array1D<Enum> buffers;
  if (buffers.fromPy(PyTuple_GetItem(args, 0))) {
    glDrawBuffers(buffers.size(), buffers);
  }
  Py_RETURN_NONE;
}
void upsample(const Array1D<T>& v, int usf, Array1D<T>& u)
{
  //  it_assert1(usf >= 1, "upsample: upsampling factor must be equal or greater than one" );
  u.set_size(v.length()*usf);
  u.clear();
  for(long i=0;i<v.length();i++)
    u(i*usf)=v(i); 
}
Array1D<T> apply_function(fT (*f)(fT), const Array1D<T>& data)
{
    Array1D<T> out(data.length());
  
    for (int i=0;i<data.length();i++) 
	out[i]=static_cast<T>(f(static_cast<fT>(data[i])));
    return out;
}
const Array1D adjust_quant_indices(const Array1D& qIndices, const int qMatrix) {
  Array1D aQIndices(qIndices.ranges());
  // Adjust all the quantisers in qIndices
  std::transform(qIndices.data(), qIndices.data()+qIndices.num_elements(),
                 aQIndices.data(),
                 std::bind2nd(std::ptr_fun(adjust_quant_index), qMatrix) );
  return aQIndices;
}
예제 #8
0
void assign_mat_ar1d(Matrix& m, const Array1D< double >& a)
{
	m.NewMatrix(1, a.dim());
	Matrix::r_iterator p_m(m.begin());
	for(long i = 0; i < a.dim(); ++i) {
		*p_m = a[i];
		++p_m;
	}
}
예제 #9
0
파일: gl_2_0.cpp 프로젝트: gatgui/pygl
static PyObject* py_glShaderSource(PyObject *, PyObject *args) {
  CHECK_ARG_COUNT(args, 2);
  Uint shader(PyTuple_GetItem(args, 0));
  Array1D<String> strings;
  if (strings.fromPy(PyTuple_GetItem(args, 1))) {
    glShaderSource(shader, strings.size(), strings, 0);
  }
  Py_RETURN_NONE;
}
Array1D<T> zero_pad(const Array1D<T>& v, int n)
{
    it_assert(n>=v.size(), "zero_pad() cannot shrink the vector!");
    Array1D<T> v2(n);
    v2.set_subvector(0, v.size()-1, v);
    if (n > v.size())
	v2.set_subvector(v.size(), n-1, T(0));

    return v2;
}
Array1D<T> repeat(const Array1D<T>& v, int norepeats)
{
    Array1D<T> temp(v.length()*norepeats);
  
    for(int i=0;i<v.length();i++) {
	for(int j=0;j<norepeats;j++)
	    temp(i*norepeats+j)=v(i);
    }
    return temp;
}
void lininterp(const Array1D<T>& v, int usf, Array1D<T>& u)
{
  //  it_assert1(usf >= 1, "lininterp: upsampling factor must be equal or greater than one" );
  long L = (v.length()-1)*usf+1;
  u.set_size(L);
  for (long j = 0; j < L-1; j++) {
    //u(j) = (v(j/usf) + (j % usf)/((float)usf)*(v((j+usf)/usf)-v(j/usf)));  
    u(j) = (v(j/usf) + (j % usf)/((double)usf)*(v((j+usf)/usf)-v(j/usf)));  
  }
  u(L-1) = v(v.length()-1);
}
 void
 CDFBasis<d,dt>::evaluate
 (const unsigned int derivative,
  const RIndex& lambda,
  const Array1D<double>& points, Array1D<double>& values)
 {
   values.resize(points.size());
   for (unsigned int i(0); i < values.size(); i++) {
     values[i] = evaluate(derivative, lambda, points[i]);
   }
 }
const Array2D inverse_quantise_transform_np(const Array2D& qCoeffs,
                                            const Array2D& qIndices,
                                            const Array1D& qMatrix) {
  // TO DO: Check numberOfSubbands=3n+1 ?
  BlockVector aQIndices(qMatrix.ranges());
  const int numberOfSubbands = qMatrix.size();
  for (char band=0; band<numberOfSubbands; ++band) {
    aQIndices[band] = adjust_quant_indices(qIndices, qMatrix[band]);
  }
  return inverse_quantise_subbands_np(qCoeffs, aQIndices);
}
예제 #15
0
Array2D CombinationByDecrease(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }
    Array1D temp(K);
    CombinationByDecreaseHelper(array, array.size(), K, temp, result);
}
예제 #16
0
/**
 * @brief find all permutation of array
 * @brief array has some repeating elements
 */
Array2D PermutationUnique(Array1D array)
{
    Array2D result;
    if (array.empty()) { return result; }
    if (array.size() == 1) {
        result.push_back(array);
        return result;
    }
    PermutationUniqueHelper(array, 0, result);
    return result;
}
예제 #17
0
Array2D CombinationVer2(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }
    Array1D temp;
    CombinationVer2Helper(array, 0, array.size() - 1, K, temp, result);
}
예제 #18
0
Array2D Combination(Array1D array, int K) {
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);
        return result;
    }
    Array1D temp;
    CombinationHelper(array, 0, array.size() - 1, K, temp, result);
    return result;
}
예제 #19
0
Array2D CombinationWithDuplicatedElementsVer2(Array1D array, int K)
{
    Array2D result;
    if (array.empty() || K <= 0) { return result; }
    if (array.size() < K) {
        result.push_back(array);  /** @warning maybe exclude it */
        return result;
    }
    Array1D temp;

}
예제 #20
0
static void PermutationHelper(Array1D &array, int index, Array2D &result)
{
    if (index == array.size()) {
        result.push_back(array);
        return;
    }
    for (int i = index; i < array.size(); ++i) {
        std::swap(array[index], array[i]);
        PermutationHelper(array, index + 1, result);
        std::swap(array[index], array[i]);
    }
}
Array1D<T> cross(const Array1D<T>& v1, const Array1D<T>& v2)
{
    it_assert( v1.size() == 3 && v2.size() == 3, "cross: vectors should be of size 3");

    Array1D<T> r(3);

    r(0) = v1(1) * v2(2) - v1(2) * v2(1);
    r(1) = v1(2) * v2(0) - v1(0) * v2(2);
    r(2) = v1(0) * v2(1) - v1(1) * v2(0);
    
    return r;
}
double variance(const Array1D<T>& v)
{
    int len = v.length();
    const T *p=v._data();
    double sum=0.0, sq_sum=0.0;

    for (int i=0; i<len; i++, p++) {
	sum += *p;
	sq_sum += *p * *p;
    }
    
    return (double)(sq_sum - sum*sum/len) / (len-1);
}
예제 #23
0
double Application::recompute(double t)
{
  SubstitutionMatrix &M=*Q->instantiate(t);
  Array1D<double> eq;
  M.getEqFreqs(eq);
  int n=eq.size();
  double r=0.0;
  for(BOOM::Symbol s=0 ; s<n ; ++s) {
    r+=eq[s]*M(s,s);
  }
  delete &M;
  return r;
}
예제 #24
0
static void PermutationUniqueHelper(Array1D &array, int index, Array2D &result)
{
    if (index == array.size()) {
        result.push_back(array);
        return;
    }
    for (int i = index; i < array.size(); i++) {
        if (HasSameElement(array, index, i, array[i])) {
            std::swap(array[index], array[i]);
            PermutationUniqueHelper(array, index + 1, result);
            std::swap(array[index], array[i]);
        }
    }
}
예제 #25
0
static void CombinationWithDuplicatedElementsVer2Helper(Array1D &array, int left,
                                                        int k, int times,
                                                        Array1D &temp,
                                                        Array2D &result)
{
    if (array.size() - left < k) { return; }
    if (k == 0) {
        result.push_back(temp);
        return;
    }
    for (int i = left; i <= array.size(); i++) {
        if (i == 0 || array[i] != array[i - 1]) {
            times = 1;
            temp.push_back(array[i]);
            CombinationWithDuplicatedElementsVer2Helper(array, i + 1, k - 1, 1, temp, result);
            temp.pop_back();
        }
        else {
            times++;
            if (temp.size() >= times - 1 && temp[temp.size() - times + 1] == array[i]) {
                temp.push_back(array[i]);
                CombinationWithDuplicatedElementsVer2Helper(array, i + 1, k - 1, times, temp, result);
                temp.pop_back();
            }
        }
    }
}
T product(const Array1D<T>& v)
{
    double lnM=0;
    for (int i=0;i<v.length();i++)
	lnM += log(static_cast<double>(v[i]));
    return static_cast<T>(exp(lnM));
}
template<class T> T sum(const Array1D<T>& v)
{
    T M=0;
    for (int i=0;i<v.length();i++)
	M+=v[i];
    return M;
}
예제 #28
0
bool NextPermutation(Array1D &array)
{
    Array1D::size_type size = array.size();
    if (size <= 1) { return false; }
    for (int i = size - 2, ii = size - 1; i >= 0; i--, ii--) {
        if (array[i] < array[ii]) {
            int j = size - 1;
            while (array[j] <= array[i]) { j--; }
            std::swap(array[i], array[j]);
            std::reverse(array.begin() + ii, array.end());
            return true;
        }
    }
    std::reverse(array.begin(), array.end());
    return false;
}
예제 #29
0
static void CombinationHelper(Array1D &array, int left, int right, int k,
                              Array1D &temp, Array2D &result)
{
    assert(!array.empty());
    assert(left >= 0 && right >= 0);

    if (right - left + 1 < k) { return; }
    if (k == 0) {
        result.push_back(temp);
        return;
    }
    temp.push_back(array[left]);
    CombinationHelper(array, left + 1, right, k - 1, temp, result);
    temp.pop_back();
    CombinationHelper(array, left + 1, right, k, temp, result);
}
예제 #30
0
  double processInternalChild(Symbol parentSymbol,int parentID,int child,
			      NthOrdSubstMatrix &noPt) {
    AlignmentSeq &track=A.getIthTrack(parentID);
    AlignmentSeq &childTrack=A.getIthTrack(child);
    int contextBegin=column-order;
    if(contextBegin<0) contextBegin=0;
    int contextLen=column-contextBegin;
    Sequence context;
    track.getSeq().getSubsequence(contextBegin,contextLen,context);//ACO
    //A.getIthTrack(root->getID()).getSeq().getSubsequence(contextBegin,contextLen,context);//TRCO
    int pos=MultSeqAlignment::rightmostGapPos(context,gapSymbols);
    if(pos>=0) {
      throw "this should not happen (FitchFelsenstein)::processInternalChild";
      Sequence temp;
      context.getSubsequence(pos+1,contextLen-pos-1,temp);
      context=temp;
    }
    SubstitutionMatrix &Pt=*noPt.lookup(context,0,-1);
    Array2D<double>::RowIn2DArray<double> row=L[child];
    Symbol childSym=childTrack.getSeq()[column];
    double ll;
    if(gapSymbols.isMember(childSym)){
      V.setAllTo(NEGATIVE_INFINITY);
      for(Symbol a=0 ; a<numAlpha ; ++a) {
	if(!gapSymbols.isMember(a)) {
	  V[(int)a]=safeAdd(row[a],Pt(parentSymbol,a));
	}
      }
      ll=sumLogProbs(V);
    }
    else {
      ll=safeAdd(row[childSym],Pt(parentSymbol,childSym));
    }
    return ll;
  }