Starting on the model.h, but need to make layer structs and structs for loss and optimizers
This commit is contained in:
@@ -0,0 +1,622 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint> //uint64_t
|
||||
|
||||
#include "utils/vector.h"
|
||||
#include "utils/matrix.h"
|
||||
|
||||
|
||||
namespace numerics::detail{
|
||||
|
||||
|
||||
//
|
||||
// ---------------- Greater - Vectors ----------------
|
||||
//
|
||||
template <typename T>
|
||||
inline void inplace_greater_serial(utils::Vector<T>& v, const T c) {
|
||||
const uint64_t n = v.size();
|
||||
|
||||
if (n == 0){
|
||||
throw std::runtime_error("inplace_greater_serial: empty vector");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
if (v[i] < c){
|
||||
v[i] = T{0};
|
||||
}
|
||||
else{
|
||||
v[i] = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_greater_serial(utils::Vector<T>& v, const utils::Vector<T>& p) {
|
||||
const uint64_t n = v.size();
|
||||
const uint64_t m = p.size();
|
||||
|
||||
if (n == 0){
|
||||
throw std::runtime_error("inplace_greater_serial: empty vector");
|
||||
}
|
||||
|
||||
if (n != m){
|
||||
throw std::runtime_error("inplace_greater_serial: vector size mismatch");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
if (v[i] < p[i]){
|
||||
v[i] = T{0};
|
||||
}
|
||||
else{
|
||||
v[i] = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// ---------------- Greater - Matrices ----------------
|
||||
//
|
||||
template <typename T>
|
||||
inline void inplace_greater_serial(utils::Matrix<T>& A, const T c) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_greater_serial: empty matrix");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) < c){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_greater_rowwise_serial(utils::Matrix<T>& A, const utils::Vector<T>& v) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_greater_rowwise_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (rows != v.size()){
|
||||
throw std::runtime_error("inplace_greater_rowwise_serial: Matrix-Vector size mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) < v[i]){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_greater_colwise_serial(utils::Matrix<T>& A, const utils::Vector<T>& v) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_greater_colwise_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (cols != v.size()){
|
||||
throw std::runtime_error("inplace_greater_colwise_serial: Matrix-Vector size mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) < v[j]){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_greater_serial(utils::Matrix<T>& A, const utils::Matrix<T>& B) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_greater_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (rows != B.rows() || cols != B.cols()){
|
||||
throw std::runtime_error("inplace_greater_serial: Matrix size mismatch");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) < B(i,j)){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// ---------------- Greater or Equal - Vectors ----------------
|
||||
//
|
||||
template <typename T>
|
||||
inline void inplace_greater_equal_serial(utils::Vector<T>& v, const T c) {
|
||||
const uint64_t n = v.size();
|
||||
|
||||
if (n == 0){
|
||||
throw std::runtime_error("inplace_greater_equal_serial: empty vector");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
if (v[i] <= c){
|
||||
v[i] = T{0};
|
||||
}
|
||||
else{
|
||||
v[i] = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_greater_equal_serial(utils::Vector<T>& v, const utils::Vector<T>& p) {
|
||||
const uint64_t n = v.size();
|
||||
const uint64_t m = p.size();
|
||||
|
||||
if (n == 0){
|
||||
throw std::runtime_error("inplace_greater_equal_serial: empty vector");
|
||||
}
|
||||
|
||||
if (n != m){
|
||||
throw std::runtime_error("inplace_greater_equal_serial: vector size mismatch");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
if (v[i] <= p[i]){
|
||||
v[i] = T{0};
|
||||
}
|
||||
else{
|
||||
v[i] = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// ---------------- Greater - Matrices ----------------
|
||||
//
|
||||
template <typename T>
|
||||
inline void inplace_greater_equal_serial(utils::Matrix<T>& A, const T c) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_greater_equal_serial: empty matrix");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) <= c){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_greater_equal_rowwise_serial(utils::Matrix<T>& A, const utils::Vector<T>& v) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_greater_equal_rowwise_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (rows != v.size()){
|
||||
throw std::runtime_error("inplace_greater_equal_rowwise_serial: Matrix-Vector size mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) <= v[i]){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_greater_equal_colwise_serial(utils::Matrix<T>& A, const utils::Vector<T>& v) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_greater_equal_colwise_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (cols != v.size()){
|
||||
throw std::runtime_error("inplace_greater_equal_colwise_serial: Matrix-Vector size mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) <= v[j]){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_greater_equal_serial(utils::Matrix<T>& A, const utils::Matrix<T>& B) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_greater_equal_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (rows != B.rows() || cols != B.cols()){
|
||||
throw std::runtime_error("inplace_greater_equal_serial: Matrix size mismatch");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) <= B(i,j)){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// ---------------- Less - Vectors ----------------
|
||||
//
|
||||
template <typename T>
|
||||
inline void inplace_less_serial(utils::Vector<T>& v, const T c) {
|
||||
const uint64_t n = v.size();
|
||||
|
||||
if (n == 0){
|
||||
throw std::runtime_error("inplace_less_serial: empty vector");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
if (v[i] > c){
|
||||
v[i] = T{0};
|
||||
}
|
||||
else{
|
||||
v[i] = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_less_serial(utils::Vector<T>& v, const utils::Vector<T>& p) {
|
||||
const uint64_t n = v.size();
|
||||
const uint64_t m = p.size();
|
||||
|
||||
if (n == 0){
|
||||
throw std::runtime_error("inplace_less_serial: empty vector");
|
||||
}
|
||||
|
||||
if (n != m){
|
||||
throw std::runtime_error("inplace_less_serial: vector size mismatch");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
if (v[i] > p[i]){
|
||||
v[i] = T{0};
|
||||
}
|
||||
else{
|
||||
v[i] = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// ---------------- Greater - Matrices ----------------
|
||||
//
|
||||
template <typename T>
|
||||
inline void inplace_less_serial(utils::Matrix<T>& A, const T c) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_less_serial: empty matrix");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) > c){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_less_rowwise_serial(utils::Matrix<T>& A, const utils::Vector<T>& v) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_less_rowwise_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (rows != v.size()){
|
||||
throw std::runtime_error("inplace_less_rowwise_serial: Matrix-Vector size mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) > v[i]){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_less_colwise_serial(utils::Matrix<T>& A, const utils::Vector<T>& v) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_less_colwise_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (cols != v.size()){
|
||||
throw std::runtime_error("inplace_less_colwise_serial: Matrix-Vector size mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) > v[j]){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_less_serial(utils::Matrix<T>& A, const utils::Matrix<T>& B) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_less_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (rows != B.rows() || cols != B.cols()){
|
||||
throw std::runtime_error("inplace_less_serial: Matrix size mismatch");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) > B(i,j)){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// ---------------- Less or Equal - Vectors ----------------
|
||||
//
|
||||
template <typename T>
|
||||
inline void inplace_less_equal_serial(utils::Vector<T>& v, const T c) {
|
||||
const uint64_t n = v.size();
|
||||
|
||||
if (n == 0){
|
||||
throw std::runtime_error("inplace_less_equal_serial: empty vector");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
if (v[i] >= c){
|
||||
v[i] = T{0};
|
||||
}
|
||||
else{
|
||||
v[i] = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_less_equal_serial(utils::Vector<T>& v, const utils::Vector<T>& p) {
|
||||
const uint64_t n = v.size();
|
||||
const uint64_t m = p.size();
|
||||
|
||||
if (n == 0){
|
||||
throw std::runtime_error("inplace_less_equal_serial: empty vector");
|
||||
}
|
||||
|
||||
if (n != m){
|
||||
throw std::runtime_error("inplace_less_equal_serial: vector size mismatch");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
if (v[i] >= p[i]){
|
||||
v[i] = T{0};
|
||||
}
|
||||
else{
|
||||
v[i] = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// ---------------- Greater - Matrices ----------------
|
||||
//
|
||||
template <typename T>
|
||||
inline void inplace_less_equal_serial(utils::Matrix<T>& A, const T c) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_less_equal_serial: empty matrix");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) >= c){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_less_equal_rowwise_serial(utils::Matrix<T>& A, const utils::Vector<T>& v) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_less_equal_rowwise_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (rows != v.size()){
|
||||
throw std::runtime_error("inplace_less_equal_rowwise_serial: Matrix-Vector size mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) >= v[i]){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_less_equal_colwise_serial(utils::Matrix<T>& A, const utils::Vector<T>& v) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_less_equal_colwise_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (cols != v.size()){
|
||||
throw std::runtime_error("inplace_less_equal_colwise_serial: Matrix-Vector size mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) >= v[j]){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
inline void inplace_less_equal_serial(utils::Matrix<T>& A, const utils::Matrix<T>& B) {
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
|
||||
if (rows == 0 || cols == 0){
|
||||
throw std::runtime_error("inplace_less_equal_serial: empty matrix");
|
||||
}
|
||||
|
||||
if (rows != B.rows() || cols != B.cols()){
|
||||
throw std::runtime_error("inplace_less_equal_serial: Matrix size mismatch");
|
||||
}
|
||||
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) >= B(i,j)){
|
||||
A(i,j) = T{0};
|
||||
}
|
||||
else{
|
||||
A(i,j) = T{1};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace numerics
|
||||
|
||||
Reference in New Issue
Block a user