Janus 2.0.0
High-performance C++20 dual-mode numerical framework
Loading...
Searching...
No Matches
Spacing.hpp
Go to the documentation of this file.
1#pragma once
6
11#include "janus/math/Trig.hpp"
12#include <Eigen/Dense>
13#include <numbers>
14
15namespace janus {
16
17// --- linspace ---
26template <typename T> JanusVector<T> linspace(const T &start, const T &end, int n) {
27 if (n < 1) {
28 throw InvalidArgument("linspace: n must be >= 1");
29 }
30 if (n == 1) {
31 JanusVector<T> ret(1);
32 ret(0) = start;
33 return ret;
34 }
35
36 JanusVector<T> ret(n);
37 // Explicit loop to support symbolic types (cannot use .setLinSpaced)
38 T step = (end - start) / static_cast<double>(n - 1);
39
40 for (int i = 0; i < n; ++i) {
41 ret(i) = start + static_cast<double>(i) * step;
42 }
43 // Ensure exact end point
44 ret(n - 1) = end;
45 return ret;
46}
47
48// --- cosine_spacing ---
57template <typename T> JanusVector<T> cosine_spacing(const T &start, const T &end, int n) {
58 if (n < 1) {
59 throw InvalidArgument("cosine_spacing: n must be >= 1");
60 }
61 if (n == 1) {
62 JanusVector<T> ret(1);
63 ret(0) = start;
64 return ret;
65 }
66
67 JanusVector<T> ret(n);
68 T center = 0.5 * (start + end);
69 T radius = 0.5 * (end - start);
70 double pi = std::numbers::pi_v<double>;
71
72 for (int i = 0; i < n; ++i) {
73 double angle = pi * static_cast<double>(i) / static_cast<double>(n - 1);
74 ret(i) = center - radius * std::cos(angle);
75 }
76 return ret;
77}
78
79// --- sinspace ---
89template <typename T>
90JanusVector<T> sinspace(const T &start, const T &end, int n, bool reverse_spacing = false) {
91 if (n < 1) {
92 throw InvalidArgument("sinspace: n must be >= 1");
93 }
94 if (n == 1) {
95 JanusVector<T> ret(1);
96 ret(0) = start;
97 return ret;
98 }
99
100 if (reverse_spacing) {
101 auto ret = sinspace(end, start, n, false);
102 return ret.reverse();
103 }
104
105 JanusVector<T> ret(n);
106 double pi_half = std::numbers::pi_v<double> / 2.0;
107
108 for (int i = 0; i < n; ++i) {
109 // angle goes from 0 to pi/2
110 double angle = pi_half * static_cast<double>(i) / static_cast<double>(n - 1);
111 double factor = 1.0 - std::cos(angle); // 0 to 1
112 ret(i) = start + (end - start) * factor;
113 }
114 // Ensure exact endpoints
115 ret(0) = start; // cos(0) = 1, factor=0
116 ret(n - 1) = end; // cos(pi/2) = 0, factor=1
117 return ret;
118}
119
120// --- logspace ---
129template <typename T> JanusVector<T> logspace(const T &start, const T &end, int n) {
130 if (n < 1) {
131 throw InvalidArgument("logspace: n must be >= 1");
132 }
133 if (n == 1) {
134 JanusVector<T> ret(1);
135 ret(0) = janus::pow(10.0, start);
136 return ret;
137 }
138
139 JanusVector<T> ret(n);
140 // Linear spacing in exponent
141 for (int i = 0; i < n; ++i) {
142 double fraction = static_cast<double>(i) / static_cast<double>(n - 1);
143 T exponents = start + (end - start) * fraction;
144 ret(i) = janus::pow(10.0, exponents);
145 }
146 // ret(0) is 10^start, ret(n-1) is 10^end
147 return ret;
148}
149
150// --- geomspace ---
159template <typename T> JanusVector<T> geomspace(const T &start, const T &end, int n) {
160 if (n < 1) {
161 throw InvalidArgument("geomspace: n must be >= 1");
162 }
163 T log_start = janus::log10(start);
164 T log_end = janus::log10(end);
165 return logspace(log_start, log_end, n);
166}
167
168} // namespace janus
Scalar and element-wise arithmetic functions (abs, sqrt, pow, exp, log, etc.).
C++20 concepts constraining valid Janus scalar types.
Custom exception hierarchy for Janus framework.
Core type aliases for numeric and symbolic Eigen/CasADi interop.
Trigonometric and inverse trigonometric functions.
Input validation failed (e.g., mismatched sizes, invalid parameters).
Definition JanusError.hpp:31
Definition Diagnostics.hpp:19
JanusVector< T > logspace(const T &start, const T &end, int n)
Generates logarithmically spaced vector (base 10).
Definition Spacing.hpp:129
T pow(const T &base, const T &exponent)
Computes the power function: base^exponent.
Definition Arithmetic.hpp:72
JanusVector< T > sinspace(const T &start, const T &end, int n, bool reverse_spacing=false)
Generates sine spaced vector (denser at start by default).
Definition Spacing.hpp:90
T log10(const T &x)
Computes the base-10 logarithm of x.
Definition Arithmetic.hpp:180
JanusVector< T > cosine_spacing(const T &start, const T &end, int n)
Generates cosine spaced vector (denser at ends).
Definition Spacing.hpp:57
JanusVector< T > linspace(const T &start, const T &end, int n)
Generates linearly spaced vector.
Definition Spacing.hpp:26
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > JanusVector
Dynamic-size column vector for both numeric and symbolic backends.
Definition JanusTypes.hpp:49
JanusVector< T > geomspace(const T &start, const T &end, int n)
Generates geometrically spaced vector.
Definition Spacing.hpp:159