AnyType get_row_from_2d_array::run(AnyType & args) { MappedMatrix input = args[0].getAs<MappedMatrix>(); int index = args[1].getAs<int>() - 1; // database index starts from 1 if (index < 0 or index >= input.cols()) { std::stringstream err_msg; err_msg << "Out-of-bound index: " << index << " >= " << input.cols(); throw std::runtime_error(err_msg.str()); } MutableNativeColumnVector ret(this->allocateArray<double>(input.rows())); ret = input.col(static_cast<Index>(index)); return ret; }
void closestColumnsAndDistances( const MappedMatrix& inMatrix, const MappedColumnVector& inVector, DistanceFunction& inMetric, RandomAccessIterator ioFirst, RandomAccessIterator ioLast) { ReverseLexicographicComparator< typename std::iterator_traits<RandomAccessIterator>::value_type> comparator; std::fill(ioFirst, ioLast, std::make_tuple(0, std::numeric_limits<double>::infinity())); for (Index i = 0; i < inMatrix.cols(); ++i) { double currentDist = AnyType_cast<double>( inMetric(MappedColumnVector(inMatrix.col(i)), inVector) ); // outIndicesAndDistances is a heap, so the first element is maximal if (currentDist < std::get<1>(*ioFirst)) { // Unfortunately, the STL does not have a decrease-key function, // so we are wasting a bit of performance here std::pop_heap(ioFirst, ioLast, comparator); *(ioLast - 1) = std::make_tuple(i, currentDist); std::push_heap(ioFirst, ioLast, comparator); } } std::sort_heap(ioFirst, ioLast, comparator); }
AnyType matrix_vec_mult_in_mem_2d::run(AnyType & args){ MappedColumnVector vec = args[0].getAs<MappedColumnVector>(); MappedMatrix mat = args[1].getAs<MappedMatrix>(); // Note mat is constructed in the column-first order // which means that mat is actually transposed if(vec.size() != mat.cols()){ throw std::invalid_argument( "dimensions mismatch: vec.size() != matrix.rows()"); }; // trans(vec) * trans(mat) = mat * vec Matrix r = mat * vec; ColumnVector v = r.col(0); return v; }
std::tuple<Index, double> closestColumnAndDistance( const MappedMatrix& inMatrix, const MappedColumnVector& inVector, DistanceFunction& inMetric) { Index closestColumn = 0; double minDist = std::numeric_limits<double>::infinity(); for (Index i = 0; i < inMatrix.cols(); ++i) { double currentDist = AnyType_cast<double>( inMetric(MappedColumnVector(inMatrix.col(i)), inVector) ); if (currentDist < minDist) { closestColumn = i; minDist = currentDist; } } return std::tuple<Index, double>(closestColumn, minDist); }