Sync public subset from Flux

This commit is contained in:
Gitea CI
2025-10-07 11:09:55 +00:00
parent 8892d58e66
commit 35023cb7e1
30 changed files with 707 additions and 229 deletions

18
include/numerics/exp.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include <cmath>
#include "./utils/vector.h"
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
T exp(const T a){
return std::exp(a);
}
} // namespace numerics

View File

@@ -1,39 +0,0 @@
#pragma once
#include <cmath>
#include "./utils/vector.h"
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
T exponential(const T a){
return std::exp(a);
}
template <typename T>
utils::Vector<T> exponential(const utils::Vector<T>& a){
utils::Vector<T> b = a;
for (uint64_t i = 0; i < a.size(); ++i){
b[i] = numerics::exponential(a[i]);
}
return b;
}
template <typename T>
utils::Matrix<T> exponential(const utils::Matrix<T>& A){
utils::Matrix<T> B = A;
for (uint64_t i = 0; i < A.rows(); ++i){
for (uint64_t j = 0; j < A.cols(); ++j){
B(i,j) = numerics::exponential(A(i,j));
}
}
return B;
}
} // namespace numerics

25
include/numerics/log.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include "./utils/vector.h"
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
void inplace_log(T a){
a = std::log(a);
}
template <typename T>
T log(const T a){
T b = a;
inplace_log(b);
return b;
}
} // namespace numerics

View File

@@ -0,0 +1,76 @@
#pragma once
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
void inplace_matclip_high(utils::Matrix<T>& A, T high){
uint64_t rows = A.rows();
uint64_t cols = A.cols();
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (A(i,j) > high){
A(i,j) = high;
}
}
}
}
template <typename T>
void inplace_matclip_low(utils::Matrix<T>& A, T low){
uint64_t rows = A.rows();
uint64_t cols = A.cols();
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (A(i,j) < low){
A(i,j) = low;
}
}
}
}
template <typename T>
void inplace_matclip(utils::Matrix<T>& A, T low, T high){
uint64_t rows = A.rows();
uint64_t cols = A.cols();
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (A(i,j) > high){
A(i,j) = high;
}else if (A(i,j) < low){
A(i,j) = low;
}
}
}
}
template <typename Td, typename Ti>
utils::Matrix<Td> matclip_high(const utils::Matrix<Ti>& A, Td high){
utils::Matrix<Td> B = A;
inplace_matclip_high(B, high);
return B;
}
template <typename Td, typename Ti>
utils::Matrix<Td> matclip_low(const utils::Matrix<Ti>& A, Td low){
utils::Matrix<Td> B = A;
inplace_matclip_low(B, low);
return B;
}
template <typename Td, typename Ti>
utils::Matrix<Td> matclip(const utils::Matrix<Ti>& A, Td low, Td high){
utils::Matrix<Td> B = A;
inplace_matclip(B, low, high);
return B;
}
} // namespace numerics

63
include/numerics/matdot.h Normal file
View File

@@ -0,0 +1,63 @@
#pragma once
#include "./core/omp_config.h"
#include "./utils/matrix.h"
#include "./utils/vector.h"
namespace numerics{
template <typename T>
utils::Vector<T> matdot_row(const utils::Matrix<T>& A, const utils::Matrix<T>& B){
if (A.rows() != B.rows() || A.cols() != B.cols()){
throw std::runtime_error("matmul: dimension mismatch");
}
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
utils::Vector<T> c(rows, T{0});
for (uint64_t i = 0; i < rows; ++i){
T sum = T{0};
for (uint64_t j = 0; j < cols; ++j){
sum += A(i,j) * A(i,j);
}
c[i] = sum;
}
return c;
}
template <typename T>
utils::Vector<T> matdot_col(const utils::Matrix<T>& A, const utils::Matrix<T>& B){
if (A.rows() != B.rows() || A.cols() != B.cols()){
throw std::runtime_error("matmul: dimension mismatch");
}
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
utils::Vector<T> c(cols, T{0});
for (uint64_t j = 0; j < cols; ++j){
T sum = T{0};
for (uint64_t i = 0; i < rows; ++i){
sum += A(i,j) * A(i,j);
}
c[j] = sum;
}
return c;
}
} // namespace numerics

22
include/numerics/matexp.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
utils::Matrix<T> matexp(const utils::Matrix<T>& A){
utils::Matrix<T> B = A;
for (uint64_t i = 0; i < A.rows(); ++i){
for (uint64_t j = 0; j < A.cols(); ++j){
B(i,j) = numerics::exp(A(i,j));
}
}
return B;
}
} // namespace numerics

34
include/numerics/matlog.h Normal file
View File

@@ -0,0 +1,34 @@
#pragma once
#include "./utils/vector.h"
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
void inplace_matlog(utils::Matrix<T>& A){
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
numerics::inplace_log(A(i,j));
}
}
}
template <typename T>
utils::Matrix<T> log(const utils::Matrix<T>& A){
utils::Matrix<T> B = A;
inplace_matlog(B);
return B;
}
} // namespace numerics

52
include/numerics/matmax.h Normal file
View File

@@ -0,0 +1,52 @@
#pragma once
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
T matmax(const utils::Matrix<T>& A){
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
T max_value(T{0});
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
max_value = numerics::max(max_value, A(i,j));
}
}
return max_value;
}
template <typename T>
utils::Vector<T> matmax(const utils::Matrix<T>& A, std::string method){
utils::Vector<T> b;
if (method == "cols"){
b.resize(A.cols(), T{0});
for (uint64_t i = 0; i < A.cols(); ++i){
for (uint64_t j = 0; j < A.rows(); ++j){
b[i] = numerics::max(A(j, i), b[i]);
}
}
}else if (method == "rows"){
b.resize(A.rows(), T{0});
for (uint64_t i = 0; i < A.rows(); ++i){
for (uint64_t j = 0; j < A.cols(); ++j){
b[i] = numerics::max(A(i, j), b[i]);
}
}
}else{
throw std::runtime_error("max: choose 'rows or 'cols'");
}
return b;
}
} // namespace numerics

View File

@@ -0,0 +1,88 @@
#ifndef _mean_n_
#define _mean_n_
#include "./utils/vector.h"
#include "./utils/matrix.h"
#include "./core/omp_config.h"
namespace numerics{
template <typename T>
T matmean(utils::Matrix<T>& A) {
T mean(T{0});
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
for (uint64_t i = 0; i < cols; ++i) {
for (uint64_t j = 0; j < rows; ++j) {
mean += A(j, i);
}
}
mean /= (static_cast<T>(rows)* static_cast<T>(cols));
return mean;
}
template <typename T>
void inplace_matmean_row(utils::Matrix<T>& A, utils::Vector<T>& b) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
if (b.size() != cols){
b.resize(cols, T{0});
}
for(uint64_t j = 0; j < cols; ++j){
for (uint64_t i = 0; i < rows; ++i){
b[j] += A(i, j);
}
b[j] =/ static_cast<T>(rows);
}
}
template <typename T>
void inplace_matmean_cols(utils::Matrix<T>& A) {
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
if (b.size() != rows){
b.resize(rows, T{0});
}
for(uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
b[i] += A(i, j);
}
b[j] =/ static_cast<T>(cols);
}
}
template <typename T>
utils::Vector<T> matmean_row(utils::Matrix<T>& A) {
utils:Vector<T> b(A.rows(), T{0});
inplace_matmean_row(A, b);
return b;
}
template <typename T>
utils::Vector<T> matmean_col(utils::Matrix<T>& A) {
utils:Vector<T> b(A.cols(), T{0});
inplace_matmean_cols(A, b);
return b;
}
} // namespace numerics
#endif // _mean_n_

View File

@@ -8,7 +8,7 @@
namespace numerics{
template <typename T>
utils::Vector<T> matsum(utils::Matrix<T>& A, std::string method) {
utils::Vector<T> matsum(const utils::Matrix<T>& A, std::string method) {
utils::Vector<T> b;

View File

@@ -1,10 +1,5 @@
#pragma once
#include "./utils/vector.h"
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
@@ -17,60 +12,5 @@ namespace numerics{
}
}
template <typename T>
void inplace_max(utils::Matrix<T>& A, const T b){
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
for (uint64_t i = 0; i < rows; ++i){
for (uint64_t j = 0; j < cols; ++j){
if (b > A(i,j)){
//std::cout << A(i,j) << std::endl;
A(i,j) = b;
//std::cout << A(i,j) << std::endl;
}
}
}
}
template <typename T>
utils::Matrix<T> max(const utils::Matrix<T>& A, const T b){
utils::Matrix<T> B = A;
inplace_max(B, b);
return B;
}
template <typename T>
utils::Vector<T> max(const utils::Matrix<T>& A, std::string method){
utils::Vector<T> b;
if (method == "cols"){
b.resize(A.cols(), T{0});
for (uint64_t i = 0; i < A.cols(); ++i){
for (uint64_t j = 0; j < A.rows(); ++j){
b[i] = max(A(j, i), b[i]);
}
}
}else if (method == "rows"){
b.resize(A.rows(), T{0});
for (uint64_t i = 0; i < A.rows(); ++i){
for (uint64_t j = 0; j < A.cols(); ++j){
//std::cout << i << ":" << j << std::endl;
b[i] = max(A(i, j), b[i]);
}
}
}else{
throw std::runtime_error("max: choose 'rows or 'cols'");
}
return b;
}
} // namespace numerics

View File

@@ -1,31 +0,0 @@
#ifndef _mean_n_
#define _mean_n_
#include "./utils/vector.h"
#include "./utils/matrix.h"
#include "./core/omp_config.h"
namespace numerics{
template <typename T>
T mean(utils::Vector<T>& A) {
T mean(T{0});
const uint64_t rows = A.rows();
const uint64_t cols = A.cols();
for (uint64_t i = 0; i < cols; ++i) {
for (uint64_t j = 0; j < rows; ++j) {
mean += A(j, i);
}
}
mean /= (static_cast<T>(rows)* static_cast<T>(cols));
return mean;
}
} // namespace numerics
#endif // _mean_n_

View File

@@ -1,21 +1,31 @@
// "./numerics/numerics.h"
#pragma once
#include "./numerics/max.h"
#include "./numerics/exp.h"
#include "./numerics/log.h"
#include "./numerics/initializers/eye.h"
#include "./numerics/matequal.h"
#include "./numerics/transpose.h"
#include "./numerics/inverse.h"
#include "./numerics/matmul.h"
#include "./numerics/matmax.h"
#include "./numerics/matdiv.h"
#include "./numerics/matvec.h"
#include "./numerics/matadd.h"
#include "./numerics/matsubtract.h"
#include "./numerics/matsum.h"
#include "./numerics/matclip.h"
#include "./numerics/matexp.h"
#include "./numerics/matlog.h"
#include "./numerics/matdot.h"
#include "./numerics/min.h"
#include "./numerics/max.h"
#include "./numerics/abs.h"
#include "./numerics/mean.h"
#include "./numerics/exponential.h"
#include "./numerics/vecclip.h"
#include "./numerics/vecexp.h"
#include "./numerics/vecmax.h"
#include "./numerics/veclog.h"
#include "./numerics/interpolation1d.h" // base

View File

@@ -0,0 +1,69 @@
#pragma once
#include "./utils/vector.h"
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
void inplace_vecclip_high(utils::Vector<T>& a, T high){
uint64_t N = a.size();
for (uint64_t i = 0; i < N; ++i){
if (a[i] > high){
a[i] = high;
}
}
}
template <typename T>
void inplace_vecclip_low(utils::Vector<T>& a, T low){
uint64_t N = a.size();
for (uint64_t i = 0; i < N; ++i){
if (a[i] < low){
a[i] = low;
}
}
}
template <typename T>
void inplace_vecclip(utils::Vector<T>& a, T low, T high){
uint64_t N = a.size();
for (uint64_t i = 0; i < N; ++i){
if (a[i] > high){
a[i] = high;
}else if (a[i] < low){
a[i] = low;
}
}
}
template <typename Td, typename Ti>
utils::Vector<Td> vecclip_high(const utils::Vector<Ti>& a, Td high){
utils::Vector<Td> b = a;
inplace_vecclip_high(b, high);
return b;
}
template <typename Td, typename Ti>
utils::Vector<Td> vecclip_low(const utils::Vector<Ti>& a, Td low){
utils::Vector<Td> b = a;
inplace_vecclip_low(b, low);
return b;
}
template <typename Td, typename Ti>
utils::Vector<Td> vecclip(const utils::Vector<Ti>& a, Td low, Td high){
utils::Vector<Td> b = a;
inplace_vecclip(b, low, high);
return b;
}
} // namespace numerics

19
include/numerics/vecexp.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include "./utils/vector.h"
namespace numerics{
template <typename T>
utils::Vector<T> vecexp(const utils::Vector<T>& a){
utils::Vector<T> b = a;
for (uint64_t i = 0; i < a.size(); ++i){
b[i] = numerics::exp(a[i]);
}
return b;
}
} // namespace numerics

30
include/numerics/veclog.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include "./utils/vector.h"
#include "./utils/matrix.h"
namespace numerics{
template <typename T>
void inplace_veclog(utils::Vector<T>& a){
const uint64_t N = a.size();
for (uint64_t i = 0; i < N; ++i){
numerics::inplace_log(a[i]);
}
}
template <typename T>
utils::Vector<T> veclog(const utils::Vector<T>& a){
utils::Vector<T> b = a;
inplace_veclog(b);
return b;
}
} // namespace numerics

22
include/numerics/vecmax.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include "./utils/vector.h"
namespace numerics{
template <typename T>
T vecmax(const utils::Vector<T>& a){
T max_value(T{0});
uint64_t N = a.size();
for (uint64_t i = 0; i < N; ++i){
max_value = numerics::max(max_value, a[i]);
}
return max_value;
}
} // namespace numerics

View File

@@ -0,0 +1,28 @@
#ifndef _mean_n_
#define _mean_n_
#include "./utils/vector.h"
#include "./utils/matrix.h"
#include "./core/omp_config.h"
namespace numerics{
template <typename T>
T vecmean(utils::Vector<T>& a) {
T mean(T{0});
const uint64_t N = a.size();
for (uint64_t i = 0; i < N; ++i) {
mean += a[i];
}
mean /= (static_cast<T>(N));
return mean;
}
} // namespace numerics
#endif // _mean_n_