33 if (!local_maxima.n_nonzero){
35 arma::uvec ind = arma::find(X == value);
39 value = local_maxima.max();
40 arma::uvec ind = arma::find(arma::vec(local_maxima) == value);
57 value = local_maxima.max();
58 if (!local_maxima.n_nonzero){
60 arma::uvec ind = arma::find(X == value);
64 value = local_maxima.max();
65 arma::uvec ind = arma::find(arma::vec(local_maxima) == value);
80 value = local_minima.min();
81 if (!local_minima.n_nonzero){
83 arma::uvec ind = arma::find(X == value);
87 value = local_minima.max();
88 arma::uvec ind = arma::find(arma::vec(local_minima) == value);
106 value = local_minima.min();
107 if (!local_minima.n_nonzero){
109 arma::uvec ind = arma::find(X == value);
113 value = local_minima.max();
114 arma::uvec ind = arma::find(arma::vec(local_minima) == value);
140 arma::uword size = B.n_cols;
141 arma::uword number = d.size();
142 arma::uword diag_size;
143 arma::uword column_size = B.n_rows;
147 output.set_size(m, n);
148 arma::colvec diagonal;
149 arma::ivec subdiagonals;
150 arma::ivec superdiagonals;
153 throw std::invalid_argument(
"spdiags: vector larger than diagonal of arma::matrix");
157 superdiagonals = d(find (d >= 0));
158 subdiagonals = d(find (d < 0));
162 for (i = 0; i < subdiagonals.size(); ++i){
164 diagonal = output.diag(k);
165 diag_size = diagonal.n_elem;
168 if (diag_size < column_size){
169 for (i = 0; i < diag_size; ++i){
170 output.diag(k)(i) = column(i);
175 for (i = 0; i < column_size; ++i){
176 output.diag(k)(i) = column(i);
181 for (i = 0; i < superdiagonals.size(); ++i){
182 k = superdiagonals(i);
183 diag_size = output.diag(k).n_elem;
185 if (diag_size < column_size){
187 for (i = diag_size; i <=0; --i){
188 output.diag(k)(i) = column(j);
194 for (i = 0; i < column_size; ++i){
195 output.diag(k)(i) = column(i);
210 double eps = arma::datum::eps;
215 tol =
std::max(X.n_rows*s(1)*eps, X.n_cols*s(1)*eps);
216 arma::uvec q1 = find(s > tol);
217 double rank = q1.n_elem;
220 return -1*U.cols(0, rank-1);
223 std::cerr <<
"orth: no basis found" << std::endl;
224 return arma::zeros(X.n_rows, X.n_cols);
239 return (a > b ? a : b);
251 return (a < b ? a : b);
261 if (X.n_rows % n == 0){
267 arma::uword new_size = std::pow(n, nextpow);
269 arma::uword extend_by = new_size - X.n_rows;
271 padding = flipud(X.row(X.n_rows-1));
274 padding = flipud(X.rows(X.n_rows - extend_by, X.n_rows-1));
277 return join_vert(X, padding);
283 return std::ceil(std::log(number) / std::log(power));
300 dX.insert_rows(0, 1,
true);
301 d2X.insert_rows(0, 2,
true);
302 }
catch(std::exception e){
303 std::cerr <<
"Error in LocalMaxima with diff calc" << std::endl;
320 arma::uvec maxima_indices, extrema_indices;
321 arma::vec d2X_extrema;
322 arma::umat locations;
324 arma::vec extrema_buf;
326 arma::uvec position_buf_vec;
327 arma::umat position_buf;
329 for (arma::uword i = 0; i < X.n_cols; ++i){
333 arma::vec search = (arma::conv_to<arma::vec>::from(dX.col(i).rows(0, X.n_rows - 2)) % arma::conv_to<arma::vec>::from(dX.col(i).rows(1, X.n_rows - 1)));
334 extrema_indices = arma::find(search < 0);
335 }
catch(std::exception e){
336 std::cerr <<
"find" << std::endl;
342 d2X_extrema = ((arma::vec) d2X.col(i)).rows(extrema_indices);
343 maxima_indices = arma::find(d2X_extrema < 0);
344 }
catch(std::exception e){
345 std::cerr <<
"find second" << std::endl;
349 position_buf_vec = extrema_indices.rows(maxima_indices);
350 position_buf.set_size(2, maxima_indices.n_rows);
351 position_buf.row(0) = position_buf_vec.t();
352 position_buf.row(1).fill(i);
355 extrema_buf = X_buf.rows(position_buf_vec);
357 values.insert_rows(values.n_rows, extrema_buf);
358 }
catch (std::exception e){
359 std::cerr <<
"values.insert_rows failed" << std::endl;
363 locations.insert_cols(locations.n_cols, position_buf);
364 }
catch(std::exception e){
365 std::cerr <<
"locations insert_Rows failed" << std::endl;
370 return arma::sp_mat(locations, values, X.n_rows, X.n_cols,
false,
false);
384 dX.insert_rows(0, 1,
true);
385 d2X.insert_rows(0, 2,
true);\
386 }
catch(std::exception e){
387 std::cerr <<
"error in LocalMinima with diff calc" << std::endl;
403 arma::uvec minima_indices, extrema_indices;
404 arma::vec d2X_extrema;
405 arma::umat locations;
407 arma::vec extrema_buf;
409 arma::uvec position_buf_vec;
410 arma::umat position_buf;
412 for (arma::uword i = 0; i < X.n_cols; ++i){
416 arma::vec search = (arma::conv_to<arma::vec>::from(dX.col(i).rows(0, X.n_rows - 2)) % arma::conv_to<arma::vec>::from(dX.col(i).rows(1, X.n_rows - 1)));
417 extrema_indices = arma::find(search < 0);
418 }
catch(std::exception e){
419 std::cerr <<
"find" << std::endl;
425 d2X_extrema = ((arma::vec) d2X.col(i)).rows(extrema_indices);
426 minima_indices = arma::find(d2X_extrema > 0);
427 }
catch(std::exception e){
428 std::cerr <<
"find second" << std::endl;
431 position_buf_vec = extrema_indices.rows(minima_indices);
432 position_buf.set_size(2, minima_indices.n_rows);
433 position_buf.row(0) = position_buf_vec.t();
434 position_buf.row(1).fill(i);
437 extrema_buf = X_buf.rows(position_buf_vec);
439 values.insert_rows(values.n_rows, extrema_buf);
440 }
catch (std::exception e){
441 std::cerr <<
"values.insert_rows failed" << std::endl;
445 locations.insert_cols(locations.n_cols, position_buf);
446 }
catch(std::exception e){
447 std::cerr <<
"locations insert_Rows failed" << std::endl;
453 if (locations.n_rows !=2 || values.n_cols != locations.n_cols)
454 out = arma::sp_mat(X.n_rows, X.n_cols);
456 out = arma::sp_mat(locations, values, X.n_rows, X.n_cols,
false,
false);
481 if (index >= n_rows*n_cols)
482 throw std::invalid_argument(
"index out of bounds");
483 i = (arma::uword) std::floor(index / n_cols);
484 j = (arma::uword) std::floor(index - i*n_cols);
489 arma::uword n_rows, arma::uword n_cols)
491 arma::umat matrix_indices(indices.n_rows, 2);
492 arma::uword row, column;
493 for(arma::uword i = 0; i < indices.n_rows; ++i){
494 position(indices(i), n_rows, n_cols, row, column);
495 matrix_indices(i, 0) = row;
496 matrix_indices(i, 1) = column;
499 return matrix_indices;
505 if (probs > 1 || probs < 0){
506 throw std::invalid_argument(
"quantile: probs must be between 0 and 1");
508 if (data.n_rows < 1){
509 throw std::invalid_argument(
"quantile: empty input vector");
512 double h = (data.n_rows - 1)*probs;
514 if (data.in_range(data.in_range(std::floor(h) && std::floor(h) + 1))){
515 return data(std::floor(h)) + (h - std::floor(h))*(data(std::floor(h) + 1) - data(std::floor(h)));
518 else if ((std::floor(h) >= data.n_rows) || (std::floor(h) + 1 >= data.n_rows)){
519 return data(data.n_rows - 1);
522 throw std::runtime_error(
"quantile not computed");
539 return ((old_average * old_count) + new_value) / (old_count + 1.0);
558 double sum_x = old_count*old_mean + new_value;
559 double sum_squares = old_count*(std::pow(old_stddev, 2.0)
560 + std::pow(old_mean, 2.0))
561 + std::pow(new_value, 2.0);
563 double new_stddev = std::sqrt(
564 ((old_count + 1.0)*sum_squares - std::pow(sum_x, 2.0))
565 / std::pow((old_count + 1.0), 2.0));
581 if(!query.is_sorted()){query = arma::sort(query);}
582 if(!target.is_sorted()){target = arma::sort(target);}
583 arma::umat closest_indices(k, query.n_rows);
585 for (arma::uword i = 0; i < query.n_rows; ++i){
586 arma::vec difference = arma::abs(target - query(i));
587 arma::uvec ind = arma::stable_sort_index(difference);
588 closest_indices.col(i) = ind.rows(0, k-1);
590 return closest_indices;
601 if (!coefs.n_rows){
throw std::invalid_argument(
"coefs empty!");}
603 for (arma::uword i = 1; i < coefs.n_rows; ++i){y += coefs(i)*std::pow(x,i);}
619 return (wl_factor * (1.0/arma::datum::c_0) * freq_factor) * x;
633 return (freq_factor * arma::datum::c_0 * wl_factor) * x;
647 return (freq_factor * arma::datum::h * energy_factor) * x;
661 return (energy_factor * (1.0/arma::datum::h) * freq_factor) * x;
674 arma::vec foo = x * wn_factor;
675 foo.transform( [](
double val) {
return (1.0/val); } );
676 return freq_factor * foo;
689 arma::vec foo = x * freq_factor;
690 foo.transform( [](
double val) {
return (1.0/val); } );
691 return wn_factor * foo;
704 arma::vec foo = x * wn_factor;
705 foo.transform( [](
double val) {
return (1.0/val); } );
706 return wl_factor * foo;
719 arma::vec foo = wl_factor * x;
720 foo.transform( [](
double val) {
return (1.0/val); } );
721 return wn_factor * foo;
733 arma::vec foo = wl_factor * x;
748 arma::vec foo = energy_factor * x;
782 arma::uword window_size;
783 arma::vec current_coefs;
784 arma::sp_mat local_maxima_col;
785 arma::sp_mat local_maxima(coefs.n_rows, coefs.n_cols);
786 for (arma::uword i = 0; i < scales.n_rows; ++i){
787 current_coefs = coefs.col(scales(i));
788 window_size = scales(i)*2 + 1;
789 window_size = (min_window_size < window_size ? window_size: min_window_size);
805 arma::mat local_min_col;
806 arma::uword length = std::ceil(X.n_rows/window_size);
807 arma::uword old_len = X.n_rows;
808 for (arma::uword j = 0; j < X.n_cols; ++j){
809 local_min_col = local_mins.col(j);
810 local_min_col.resize(length, window_size);
811 arma::vec lowest =
arma::min(local_min_col, 1);
812 for (arma::uword i = 0; i < local_min_col.n_rows; ++i){
813 arma::uvec ind = arma::find(local_min_col.row(i) != arma::as_scalar(lowest.row(i)));
814 local_min_col.elem(ind).zeros();
816 local_min_col.resize(old_len, 1);
817 local_mins.col(j) = arma::sp_vec(local_min_col);
834 arma::mat local_max_col;
835 arma::uword length = std::ceil(X.n_rows/window_size);
836 arma::uword old_len = X.n_rows;
837 for (arma::uword j = 0; j < X.n_cols; ++j){
838 local_max_col = local_maxes.col(j);
839 local_max_col.resize(length, window_size);
840 arma::vec highest =
arma::max(local_max_col, 1);
841 for (arma::uword i = 0; i < local_max_col.n_rows; ++i){
842 arma::uvec ind = arma::find(local_max_col.row(i) != arma::as_scalar(highest.row(i)));
843 local_max_col.elem(ind).zeros();
845 local_max_col.resize(old_len, 1);
846 local_maxes.col(j) = arma::sp_mat(local_max_col);
860 if (a.n_rows != b.n_rows){
866 equal = a(i) == b(i);
868 }
while(equal && (i < a.n_rows));
880 double val = deriv(0);
884 equal = (deriv(i++) == val);
885 }
while(equal && (i < deriv.n_rows));
900 positive = (deriv(i++) > 0);
901 }
while(positive && (i < deriv.n_rows));
913 return arma::cx_mat(arma::zeros(m, n), arma::zeros(m, n));
923 return arma::cx_vec(arma::zeros(n), arma::zeros(n));
934 double delta = std::abs(vector(1) - vector(0));
935 arma::uvec indices = arma::find(((value-delta) <= vector) && (vector <= (value+delta)));
939 if (indices.n_elem) index = indices(0);
940 else if (!indices.n_elem && value < vector.min()) index = 0;
941 else index = vector.n_elem - 1;
957 if (center_type ==
"centroid")
958 center = arma::mean(spectra, 1);
959 else if (center_type ==
"medoid")
960 center = arma::median(spectra, 1);
961 else throw std::invalid_argument(
"center must be either centroid or medoid");
965 arma::vec distances(spectra.n_cols);
968 #pragma omp parallel for default(none) \ 969 shared(spectra, metric, distances, center) 970 for (intmax_t i = 0; i < (intmax_t) spectra.n_cols; ++i)
972 #pragma omp parallel for default(none) \ 973 shared(spectra, metric, distances, center) 974 for (
size_t i = 0; i < spectra.n_cols; ++i)
977 arma::vec spectrum = spectra.col(i);
978 distances(i) = metric.
Evaluate(center, spectrum);
980 index = distances.index_min();
981 return spectra.col(index);
992 std::vector<arma::uword> intersection;
993 std::sort(x.begin(), x.end());
994 std::sort(y.begin(), y.end());
995 std::set_intersection(x.begin(), x.end(),
997 std::back_inserter(intersection));
998 return arma::conv_to<arma::uvec>::from(intersection);
1009 const arma::vec &fit,
1010 arma::vec &residuals)
1012 residuals = data - fit;
1013 arma::vec centered = data - arma::as_scalar(arma::mean(data));
1014 double residual_sumsq = arma::accu(arma::pow(residuals, 2.0));
1015 double total_sumsq = arma::accu(arma::pow(centered, 2.0));
1016 return 1.0 - (residual_sumsq/total_sumsq);
1037 else if (a == b && a < x.n_rows)
return x.row(a);
1038 else if (a < x.n_rows && b >= x.n_rows)
return x.rows(a, x.n_rows - 1);
1039 else if (a < x.n_rows)
return x.rows(a, b);
1040 return x.row(x.n_rows - 1);
VESPUCCI_EXPORT arma::vec WavenumberToWavelength(const arma::vec &x, double wn_factor, double wl_factor)
Vespucci::Math::WavenumberToWavelength.
VESPUCCI_EXPORT arma::mat orth(arma::mat X)
orth Returns an orthonormal basis of the range space of A
VESPUCCI_EXPORT arma::vec WavelengthToEnergy(const arma::vec &x, double energy_factor, double wl_factor)
Vespucci::Math::WavelengthToEnergy.
VESPUCCI_EXPORT arma::vec EnergyToWavelength(const arma::vec &x, double wl_factor, double energy_factor)
Vespucci::Math::EnergyToWavelength.
VESPUCCI_EXPORT bool IsMonotonic(const arma::vec &x)
IsMonotonic.
VESPUCCI_EXPORT arma::sp_mat LocalMaximaWindow(const arma::mat &X, const int window_size)
Vespucci::Math::LocalMaximaWindow "Cleans" local maximum by window search to remove extraneous values...
VESPUCCI_EXPORT arma::vec ExtendToNextPow(arma::vec X, arma::uword n)
VESPUCCI_EXPORT bool AreEqual(const arma::vec &a, const arma::vec &b)
AreEqual.
VESPUCCI_EXPORT double RecalculateAverage(double new_value, double old_average, double old_count)
Vespucci::Math::RecalculateAverage Recalculate the average value when a new value is added to a list ...
VESPUCCI_EXPORT arma::uword max(arma::uword a, arma::uword b)
Vespucci::Math::max.
T diff(const T &X, arma::uword deriv_order=1)
Vespucci::Math::diff.
VESPUCCI_EXPORT arma::vec EnergyToFrequency(const arma::vec &x, double freq_factor, double energy_factor)
Vespucci::Math::EnergyToFrequency.
VESPUCCI_EXPORT arma::vec RepresentativeSpectrum(const arma::mat &spectra, arma::uword &index, std::string metric_name="euclidean", std::string center="centroid")
Vespucci::Math::RepresentativeSpectrum.
VESPUCCI_EXPORT arma::vec WavenumberToEnergy(const arma::vec &x, double energy_factor, double wn_factor)
Vespucci::Math::WavenumberToEnergy.
VESPUCCI_EXPORT double CalculateRSquared(const arma::vec &data, const arma::vec &fit, arma::vec &residuals)
Vespucci::Math::CalculateRSquared.
VESPUCCI_EXPORT arma::vec WavelengthToFrequency(const arma::vec &x, double freq_factor, double wl_factor)
Vespucci::Math::WavelengthToFrequency.
VESPUCCI_EXPORT void position(arma::uword index, arma::uword n_rows, arma::uword n_cols, arma::uword &i, arma::uword &j)
Vespucci::Math::position Find row and column numbers for index.
VESPUCCI_EXPORT arma::uword LocalMinimum(const arma::mat &X, double &value)
Vespucci::Math::LocalMinimum.
VESPUCCI_EXPORT arma::mat spdiags(arma::mat B, arma::ivec d, arma::uword m, arma::uword n)
spdiags analgous to the Octave/arma::matLAB function A = spdiags(B, d, m, n).
VESPUCCI_EXPORT arma::uword LocalMaximum(const arma::vec &X, double &value)
Vespucci::Math::LocalMaximum.
VESPUCCI_EXPORT arma::uvec Intersection(arma::uvec &x, arma::uvec &y)
Vespucci::Math::Intersection.
VESPUCCI_EXPORT double RecalculateStdDev(double new_value, double old_mean, double old_stddev, double old_count)
Vespucci::Math::RecalculateStdDev Recalculate a standard deviation when a new value is added to a lis...
VESPUCCI_EXPORT arma::sp_mat LocalMinima(const arma::mat &X)
Vespucci::Math::LocalMinima.
VESPUCCI_EXPORT bool IsIncreasing(const arma::vec &x)
IsIncreasing.
VESPUCCI_EXPORT arma::mat SafeRows(const arma::mat &x, arma::uword a, arma::uword b)
Vespucci::Math::SafeRows.
VESPUCCI_EXPORT arma::sp_mat LocalMaxima(const arma::mat &X)
Vespucci::Math::LocalMaxima.
double Evaluate(arma::vec &first, arma::vec &second)
Vespucci::Math::DistanceMetricWrapper::Evaluate.
VESPUCCI_EXPORT arma::vec FrequencyToEnergy(const arma::vec &x, double energy_factor, double freq_factor)
Vespucci::Math::FrequencyToEnergy.
VESPUCCI_EXPORT arma::vec FrequencyToWavenumber(const arma::vec &x, double wn_factor, double freq_factor)
Vespucci::Math::FrequencyToWavenumber.
VESPUCCI_EXPORT arma::uword min(arma::uword a, arma::uword b)
Vespucci::Math::min.
VESPUCCI_EXPORT arma::sp_mat LocalMinimaWindow(const arma::mat &X, const int window_size)
Vespucci::Math::LocalMinimaWindow.
VESPUCCI_EXPORT double CalcPoly(const double x, const arma::vec &coefs)
Vespucci::Math::CalcPoly Calculate the value of a polynomial at particular point. ...
VESPUCCI_EXPORT arma::vec EnergyToWavenumber(const arma::vec &x, double wn_factor, double energy_factor)
Vespucci::Math::EnergyToWavenumber.
VESPUCCI_EXPORT arma::sp_mat LocalMaximaCWT(arma::mat coefs, arma::uvec scales, arma::uword min_window_size)
VESPUCCI_EXPORT arma::uword ClosestIndex(double value, const arma::vec &vector)
Vespucci::Math::ClosestIndex.
VESPUCCI_EXPORT arma::uword NextPow(arma::uword number, arma::uword power)
VESPUCCI_EXPORT arma::umat GetClosestValues(arma::vec query, arma::vec target, const arma::uword k=5)
Vespucci::Math::GetClosestValues.
VESPUCCI_EXPORT arma::vec WavelengthToWavenumber(const arma::vec &x, double wl_factor, double wn_factor)
Vespucci::Math::WavelengthToWavenumber.
VESPUCCI_EXPORT double quantile(arma::vec &data, double probs)
VESPUCCI_EXPORT arma::cx_vec cx_zeros(arma::uword n)
Vespucci::Math::cx_zeros.
VESPUCCI_EXPORT arma::umat to_row_column(arma::uvec indices, arma::uword n_rows, arma::uword n_cols)
VESPUCCI_EXPORT arma::vec FrequencyToWavelength(const arma::vec &x, double wl_factor, double freq_factor)
Vespucci::Math::FrequencyToWavelength.
VESPUCCI_EXPORT arma::vec WavenumberToFrequency(const arma::vec &x, double freq_factor, double wn_factor)
Vespucci::Math::WavenumberToFrequency.