Polynomial#
-
class numpy::Polynomial#
Polynomial operations class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use Polynomial
Polynomial obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
NP_POLYNOMIAL.H:207 |
|
|
NP_POLYNOMIAL.H:219 |
|
|
NP_POLYNOMIAL.H:222 |
|
|
NP_POLYNOMIAL.H:226 |
Operators#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
Polynomial & |
NP_POLYNOMIAL.H:223 |
|
|
Polynomial & noexcept |
NP_POLYNOMIAL.H:227 |
|
|
T |
NP_POLYNOMIAL.H:353 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:378 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:392 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:406 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:426 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:436 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:440 |
|
|
Polynomial<T>& |
NP_POLYNOMIAL.H:445 |
|
|
Polynomial<T>& |
NP_POLYNOMIAL.H:450 |
|
|
Polynomial<T>& |
NP_POLYNOMIAL.H:455 |
|
|
Polynomial<T>& |
NP_POLYNOMIAL.H:460 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:599 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:612 |
|
|
bool |
NP_POLYNOMIAL.H:661 |
|
|
bool |
NP_POLYNOMIAL.H:673 |
Array Creation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
Polynomial<T> |
NP_POLYNOMIAL.H:693 |
|
|
std::pair<NDArray<T>,NDArray<T>> |
NP_POLYNOMIAL.H:730 |
|
|
void |
NP_POLYNOMIAL.H:178 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:824 |
Indexing / Selection#
Math Operations#
Type Handling#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
TargetType |
NP_POLYNOMIAL.H:842 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
Polynomial<T> |
NP_POLYNOMIAL.H:683 |
|
|
const std::vector<T>& |
NP_POLYNOMIAL.H:340 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:700 |
|
|
size_t |
NP_POLYNOMIAL.H:344 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:489 |
|
|
std::pair<Polynomial<T>,Polynomial<T>> |
NP_POLYNOMIAL.H:625 |
|
|
const std::array<T,2>& |
NP_POLYNOMIAL.H:341 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:249 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:233 |
|
|
bool |
NP_POLYNOMIAL.H:777 |
|
|
bool |
NP_POLYNOMIAL.H:803 |
|
|
bool |
NP_POLYNOMIAL.H:816 |
|
|
bool |
NP_POLYNOMIAL.H:809 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:522 |
|
|
T |
NP_POLYNOMIAL.H:188 |
|
|
T |
NP_POLYNOMIAL.H:196 |
|
|
Polynomial<T> |
NP_POLYNOMIAL.H:642 |
|
|
std::vector<std::complex<T>> |
NP_POLYNOMIAL.H:555 |
|
|
void |
NP_POLYNOMIAL.H:348 |
|
|
void |
NP_POLYNOMIAL.H:350 |
|
|
void |
NP_POLYNOMIAL.H:349 |
|
|
void |
NP_POLYNOMIAL.H:323 |
|
|
void |
NP_POLYNOMIAL.H:331 |
|
|
size_t |
NP_POLYNOMIAL.H:345 |
|
|
const std::string & |
NP_POLYNOMIAL.H:343 |
|
|
std::string |
NP_POLYNOMIAL.H:948 |
|
|
std::string |
NP_POLYNOMIAL.H:854 |
|
|
std::string |
NP_POLYNOMIAL.H:901 |
|
|
const std::array<T,2>& |
NP_POLYNOMIAL.H:342 |
Code Examples#
The following examples are extracted from the test suite.
identity (np_test_1_all.cpp:24002)
23992 throw std::runtime_error(description);
23993 }
23994 if (!((eye_offset.getShape() == std::vector<size_t>{3, 4}))) {
23995 std::string description = std::string("testArrayCreationSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((eye_offset.getShape() == std::vector<size_t>{3, 4}))";
23996 std::cout << std::string("[FAIL] ") + description << std::endl;
23997 throw std::runtime_error(description);
23998 }
23999 // std::cout << "[OK] eye function with optional parameters works correctly\n";
24000
24001 // Test identity
24002 auto identity_result = identity<int>(4);
24003 if (!((identity_result.getShape() == std::vector<size_t>{4, 4}))) {
24004 std::string description = std::string("testArrayCreationSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((identity_result.getShape() == std::vector<size_t>{4, 4}))";
24005 std::cout << std::string("[FAIL] ") + description << std::endl;
24006 throw std::runtime_error(description);
24007 }
24008 // std::cout << "[OK] identity function has correct signature\n";
24009
24010 // Test diag with different use cases
24011 auto vector1d = createInt32Array({ 3 }, 5);
24012 auto matrix2d = createInt32Array({ 3, 3 }, 0);
linspace (np_test_1_all.cpp:23001)
22991 // assert(std::abs(p_roots[2] - 1.0) < 1e-10);
22992
22993 // std::cout << "[OK] Polynomial utilities work correctly" << std::endl;
22994 std::cout << " -> tests passed" << std::endl;
22995 }
22996
22997 void test_polynomial_fitting() {
22998 std::cout << "========= test_polynomial_fitting =======================";
22999
23000 // Create test data for y = 1 + 2x + 3x^2
23001 auto x = numpy::linspace<double>(-1.0, 1.0, 10);
23002 auto y_true = numpy::NDArray<double>::createZeros({ 10 });
23003
23004 for (size_t i = 0; i < 10; ++i) {
23005 double x_val = x.getElementAt({ i });
23006 double y_val = 1.0 + 2.0 * x_val + 3.0 * x_val * x_val;
23007 y_true.setElementAt({ i }, y_val);
23008 }
23009
23010 // Fit polynomial
23011 auto fitted_poly = numpy::Polynomial<double>::fit(x, y_true, 2);
trim (np_test_5_all.cpp:842)
832 std::cout << " -> tests passed" << std::endl;
833 }
834#endif
835
836 // ============================================================================
837 // I/O UTILITIES TESTS
838 // ============================================================================
839
840 void np_test_io_trim() {
841 std::cout << "========= trim() utility =======================================";
842
843 // trim() might be in namespace or as a helper function
844 // Test basic string trimming
845 std::string test1 = " hello ";
846 std::string test2 = "\t\nworld\t\n";
847 std::string test3 = "no_trim";
848
849 // Since trim may not be directly accessible, we'll test the concept
850 auto trim = [](const std::string& str) {
851 size_t start = str.find_first_not_of(" \t\n\r");
trim_public (np_test_5_all.cpp:7618)
7608 throw std::runtime_error("np_test_has_same_methods failed");
7609 }
7610
7611 std::cout << " -> tests passed" << std::endl;
7612 }
7613
7614 void np_test_trim_public() {
7615 std::cout << "========= trim_public: remove trailing zeros =======================";
7616
7617 numpy::Polynomial<double> p({ 1.0, 2.0, 0.0, 0.0 });
7618 auto trimmed = p.trim_public();
7619
7620 auto coefs = trimmed.coefficients();
7621 bool passed = (coefs.size() == 2);
7622
7623 if (!passed) {
7624 std::cout << " [FAIL] : trim_public failed";
7625 throw std::runtime_error("np_test_trim_public failed");
7626 }
7627
7628 std::cout << " -> tests passed" << std::endl;
get_default_printstyle (np_test_5_all.cpp:8998)
8988 // Test 2: Switch to Unicode via static method
8989 numpy::Polynomial<double>::set_default_printstyle("unicode");
8990 std::string result2 = p1.toString();
8991 // std::cout << "Static method Unicode: " << result2 << std::endl;
8992
8993 if (!verify_contains(result2, "\u00B7", "Static method Unicode")) {
8994 throw std::runtime_error("Polynomial<T>::set_default_printstyle('unicode') failed");
8995 }
8996
8997 // Test 3: Verify Polynomial<T>::get_default_printstyle() static method
8998 std::string current_style = numpy::Polynomial<double>::get_default_printstyle();
8999 // std::cout << "Current style via static method: " << current_style << std::endl;
9000
9001 if (current_style != "unicode") {
9002 throw std::runtime_error("Polynomial<T>::get_default_printstyle() returned wrong value");
9003 }
9004
9005 // Test 4: Verify all polynomial types share the same global print style
9006 numpy::Chebyshev<double>::set_default_printstyle("ascii");
9007 numpy::Chebyshev<double> cheb({ 1.0, 2.0 });
get_printoptions (np_test_2_all.cpp:21259)
21249 namespace numpy_tests_printoptions {
21250
21251 // ============================================================================
21252 // BASIC FUNCTIONALITY TESTS
21253 // ============================================================================
21254
21255 void np_test_printoptions_basic() {
21256 std::cout << "========= printoptions: basic get/set ====";
21257
21258 // Get default options
21259 auto default_opts = numpy::get_printoptions();
21260
21261 // Verify defaults
21262 if (default_opts.precision != 8) {
21263 std::cout << " [FAIL] : in np_test_printoptions_basic() : default precision should be 8, got "
21264 << default_opts.precision << std::endl;
21265 throw std::runtime_error("np_test_printoptions_basic failed: default precision incorrect");
21266 }
21267
21268 if (default_opts.threshold != 1000) {
21269 std::cout << " [FAIL] : in np_test_printoptions_basic() : default threshold should be 1000, got "
power (np_test_1_all.cpp:23257)
23247 // std::cout << "[OK] Laguerre polynomials work correctly" << std::endl;
23248 std::cout << " -> tests passed" << std::endl;
23249 }
23250
23251 void test_polynomial_advanced() {
23252 std::cout << "========= test_polynomial_advanced =======================";
23253
23254 // Test polynomial power
23255 numpy::Polynomial<double> p({ 1, 1 }); // 1 + x
23256 auto p_squared = p.power(2); // (1 + x)^2 = 1 + 2x + x^2
23257 if (!((p_squared.coefficients() == std::vector<double>{1, 2, 1}))) {
23258 std::string description = std::string("test_polynomial_advanced():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((p_squared.coefficients() == std::vector<double>{1, 2, 1}))";
23259 std::cout << std::string("[FAIL] ") + description << std::endl;
23260 throw std::runtime_error(description);
23261 }
23262
23263 // Test polynomial division
23264 std::vector<double> dividend = { -1, 0, 1 }; // x^2 - 1
23265 std::vector<double> divisor = { -1, 1 }; // x - 1
23266 auto [quotient, remainder] = numpy::polydiv(dividend, divisor);
truncate (np_test_5_all.cpp:7539)
7529 throw std::runtime_error("np_test_cutdeg failed");
7530 }
7531
7532 std::cout << " -> tests passed" << std::endl;
7533 }
7534
7535 void np_test_truncate() {
7536 std::cout << "========= truncate: truncate to size =======================";
7537
7538 numpy::Polynomial<double> p({ 1.0, 2.0, 3.0, 4.0, 5.0 });
7539 auto truncated = p.truncate(3); // Keep only first 3 coefficients
7540
7541 auto coefs = truncated.coefficients();
7542 bool passed = (coefs.size() == 3 &&
7543 std::abs(coefs[0] - 1.0) < 1e-10 &&
7544 std::abs(coefs[1] - 2.0) < 1e-10 &&
7545 std::abs(coefs[2] - 3.0) < 1e-10);
7546
7547 if (!passed) {
7548 std::cout << " [FAIL] : truncate failed";
7549 throw std::runtime_error("np_test_truncate failed");
cast (np_test_5_all.cpp:7638)
7628 std::cout << " -> tests passed" << std::endl;
7629 }
7630
7631 void np_test_cast() {
7632 std::cout << "========= cast: convert polynomial type =======================";
7633
7634 numpy::Polynomial<double> p({ 1.0, 2.0, 3.0 });
7635 std::array<double, 2> new_domain = { 0.0, 5.0 };
7636 std::array<double, 2> new_window = { -2.0, 2.0 };
7637
7638 auto casted = p.cast(new_domain, new_window);
7639
7640 bool passed = (std::abs(casted.domain()[0] - new_domain[0]) < 1e-10 &&
7641 std::abs(casted.domain()[1] - new_domain[1]) < 1e-10 &&
7642 std::abs(casted.window()[0] - new_window[0]) < 1e-10 &&
7643 std::abs(casted.window()[1] - new_window[1]) < 1e-10);
7644
7645 if (!passed) {
7646 std::cout << " [FAIL] : cast failed";
7647 throw std::runtime_error("np_test_cast failed");
7648 }
basis (np_test_5_all.cpp:7482)
7472#include <cmath>
7473#include "../numpy/np_polynomial.h"
7474
7475namespace numpy_tests {
7476
7477 namespace numpy_tests_polynomial_methods {
7478
7479 void np_test_basis() {
7480 std::cout << "========= basis: generate basis polynomial =======================";
7481
7482 auto b0 = numpy::Polynomial<double>::basis(0); // Should be [1]
7483 auto b1 = numpy::Polynomial<double>::basis(1); // Should be [0, 1]
7484 auto b2 = numpy::Polynomial<double>::basis(2); // Should be [0, 0, 1]
7485
7486 bool passed = (b0.coefficients().size() == 1 && b0.coefficients()[0] == 1.0 &&
7487 b1.coefficients().size() == 2 && b1.coefficients()[1] == 1.0 &&
7488 b2.coefficients().size() == 3 && b2.coefficients()[2] == 1.0);
7489
7490 if (!passed) {
7491 std::cout << " [FAIL] : basis polynomials incorrect";
7492 throw std::runtime_error("np_test_basis failed");
coefficients (np_test_1_all.cpp:22719)
22709 void test_polynomial_creation() {
22710 std::cout << "========= test_polynomial_creation =======================";
22711
22712 // Test default constructor
22713 numpy::Polynomial<double> p1;
22714 if (!(p1.degree() == 0)) {
22715 std::string description = std::string("test_polynomial_creation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(p1.degree() == 0)";
22716 std::cout << std::string("[FAIL] ") + description << std::endl;
22717 throw std::runtime_error(description);
22718 }
22719 if (!((p1.coefficients() == std::vector<double>{0.0}))) {
22720 std::string description = std::string("test_polynomial_creation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((p1.coefficients() == std::vector<double>{0.0}))";
22721 std::cout << std::string("[FAIL] ") + description << std::endl;
22722 throw std::runtime_error(description);
22723 }
22724
22725 // Test coefficient constructor
22726 numpy::Polynomial<double> p2({ 1, 2, 3 }); // 1 + 2x + 3x^2
22727 if (!(p2.degree() == 2)) {
22728 std::string description = std::string("test_polynomial_creation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(p2.degree() == 2)";
22729 std::cout << std::string("[FAIL] ") + description << std::endl;
cutdeg (np_test_5_all.cpp:7519)
7509 throw std::runtime_error("np_test_identity failed");
7510 }
7511
7512 std::cout << " -> tests passed" << std::endl;
7513 }
7514
7515 void np_test_cutdeg() {
7516 std::cout << "========= cutdeg: truncate to degree =======================";
7517
7518 numpy::Polynomial<double> p({ 1.0, 2.0, 3.0, 4.0 }); // 1 + 2x + 3x^2 + 4x^3
7519 auto truncated = p.cutdeg(2); // Should be 1 + 2x + 3x^2
7520
7521 auto coefs = truncated.coefficients();
7522 bool passed = (coefs.size() == 3 &&
7523 std::abs(coefs[0] - 1.0) < 1e-10 &&
7524 std::abs(coefs[1] - 2.0) < 1e-10 &&
7525 std::abs(coefs[2] - 3.0) < 1e-10);
7526
7527 if (!passed) {
7528 std::cout << " [FAIL] : cutdeg failed";
7529 throw std::runtime_error("np_test_cutdeg failed");
degree (np_test_1_all.cpp:22714)
22704namespace numpy_tests {
22705
22706 using namespace numpy;
22707
22708 void test_polynomial_creation() {
22709 std::cout << "========= test_polynomial_creation =======================";
22710
22711 // Test default constructor
22712 numpy::Polynomial<double> p1;
22713 if (!(p1.degree() == 0)) {
22714 std::string description = std::string("test_polynomial_creation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(p1.degree() == 0)";
22715 std::cout << std::string("[FAIL] ") + description << std::endl;
22716 throw std::runtime_error(description);
22717 }
22718 if (!((p1.coefficients() == std::vector<double>{0.0}))) {
22719 std::string description = std::string("test_polynomial_creation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((p1.coefficients() == std::vector<double>{0.0}))";
22720 std::cout << std::string("[FAIL] ") + description << std::endl;
22721 throw std::runtime_error(description);
22722 }
derivative (np_test_1_all.cpp:22884)
22874 std::cout << " -> tests passed" << std::endl;
22875 }
22876
22877 void test_polynomial_calculus() {
22878 std::cout << "========= test_polynomial_calculus =======================";
22879
22880 // Test derivative: p(x) = 1 + 2x + 3x^2 + 4x^3
22881 numpy::Polynomial<double> p({ 1, 2, 3, 4 });
22882
22883 // First derivative: p'(x) = 2 + 6x + 12x^2
22884 auto p_deriv = p.derivative();
22885 if (!((p_deriv.coefficients() == std::vector<double>{2, 6, 12}))) {
22886 std::string description = std::string("test_polynomial_calculus():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((p_deriv.coefficients() == std::vector<double>{2, 6, 12}))";
22887 std::cout << std::string("[FAIL] ") + description << std::endl;
22888 throw std::runtime_error(description);
22889 }
22890
22891 // Second derivative: p''(x) = 6 + 24x
22892 auto p_deriv2 = p.derivative(2);
22893 if (!((p_deriv2.coefficients() == std::vector<double>{6, 24}))) {
22894 std::string description = std::string("test_polynomial_calculus():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((p_deriv2.coefficients() == std::vector<double>{6, 24}))";
divmod (np_test_1_all.cpp:21140)
21130 auto a = NDArray<float64>({ 3 });
21131 a.setElementAt({ 0 }, 7.0);
21132 a.setElementAt({ 1 }, -7.0);
21133 a.setElementAt({ 2 }, 7.5);
21134
21135 auto b = NDArray<float64>({ 3 });
21136 b.setElementAt({ 0 }, 3.0);
21137 b.setElementAt({ 1 }, 3.0);
21138 b.setElementAt({ 2 }, 2.0);
21139
21140 auto [quotient, remainder] = divmod(a, b);
21141
21142 if (!(quotient.getElementAt({ 0 }) == 2.0)) {
21143 std::string description = std::string("testDivmodFunction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(quotient.getElementAt({ 0 }) == 2.0)";
21144 std::cout << std::string("[FAIL] ") + description << std::endl;
21145 throw std::runtime_error(description);
21146 }
21147 if (!(std::abs(remainder.getElementAt({ 0 }) - 1.0) < 1e-10)) {
21148 std::string description = std::string("testDivmodFunction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(remainder.getElementAt({ 0 }) - 1.0) < 1e-10)";
21149 std::cout << std::string("[FAIL] ") + description << std::endl;
21150 throw std::runtime_error(description);
domain (np_test_5_all.cpp:7640)
7630 void np_test_cast() {
7631 std::cout << "========= cast: convert polynomial type =======================";
7632
7633 numpy::Polynomial<double> p({ 1.0, 2.0, 3.0 });
7634 std::array<double, 2> new_domain = { 0.0, 5.0 };
7635 std::array<double, 2> new_window = { -2.0, 2.0 };
7636
7637 auto casted = p.cast(new_domain, new_window);
7638
7639 bool passed = (std::abs(casted.domain()[0] - new_domain[0]) < 1e-10 &&
7640 std::abs(casted.domain()[1] - new_domain[1]) < 1e-10 &&
7641 std::abs(casted.window()[0] - new_window[0]) < 1e-10 &&
7642 std::abs(casted.window()[1] - new_window[1]) < 1e-10);
7643
7644 if (!passed) {
7645 std::cout << " [FAIL] : cast failed";
7646 throw std::runtime_error("np_test_cast failed");
7647 }
7648
7649 std::cout << " -> tests passed" << std::endl;
fit (np_test_1_all.cpp:23011)
23001 auto x = numpy::linspace<double>(-1.0, 1.0, 10);
23002 auto y_true = numpy::NDArray<double>::createZeros({ 10 });
23003
23004 for (size_t i = 0; i < 10; ++i) {
23005 double x_val = x.getElementAt({ i });
23006 double y_val = 1.0 + 2.0 * x_val + 3.0 * x_val * x_val;
23007 y_true.setElementAt({ i }, y_val);
23008 }
23009
23010 // Fit polynomial
23011 auto fitted_poly = numpy::Polynomial<double>::fit(x, y_true, 2);
23012
23013 // Debug output
23014 // std::cout << "Fitted coefficients: ["
23015 // << fitted_poly.coefficients()[0] << ", "
23016 // << fitted_poly.coefficients()[1] << ", "
23017 // << fitted_poly.coefficients()[2] << "]" << std::endl;
23018
23019 // Check that fitted coefficients are close to original
23020 // TODO: Fix fitting algorithm - temporarily disabled
23021 // assert(std::abs(fitted_poly.coefficients()[0] - 1.0) < 1e-10);
fromRoots (np_test_1_all.cpp:22752)
22742 std::cout << std::string("[FAIL] ") + description << std::endl;
22743 throw std::runtime_error(description);
22744 }
22745 if (!((p3.coefficients() == std::vector<double>{5.0}))) {
22746 std::string description = std::string("test_polynomial_creation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((p3.coefficients() == std::vector<double>{5.0}))";
22747 std::cout << std::string("[FAIL] ") + description << std::endl;
22748 throw std::runtime_error(description);
22749 }
22750
22751 // Test polynomial from roots
22752 auto p4 = numpy::Polynomial<double>::fromRoots({ 1.0, -1.0 }); // (x-1)(x+1) = x^2 - 1
22753 if (!(p4.degree() == 2)) {
22754 std::string description = std::string("test_polynomial_creation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(p4.degree() == 2)";
22755 std::cout << std::string("[FAIL] ") + description << std::endl;
22756 throw std::runtime_error(description);
22757 }
22758 if (!(std::abs(p4.coefficients()[0] + 1.0) < 1e-10)) {
22759 std::string description = std::string("test_polynomial_creation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::abs(p4.coefficients()[0] + 1.0) < 1e-10)";
22760 std::cout << std::string("[FAIL] ") + description << std::endl;
22761 throw std::runtime_error(description);
22762 }
has_samecoef (np_test_5_all.cpp:7602)
7592 std::cout << " -> tests passed" << std::endl;
7593 }
7594
7595 void np_test_has_same_methods() {
7596 std::cout << "========= has_same* methods =======================";
7597
7598 numpy::Polynomial<double> p1({ 1.0, 2.0 });
7599 numpy::Polynomial<double> p2({ 1.0, 2.0 });
7600 numpy::Polynomial<double> p3({ 1.0, 3.0 });
7601
7602 bool passed = (p1.has_samecoef(p2) && !p1.has_samecoef(p3) &&
7603 p1.has_samedomain(p2) && p1.has_samewindow(p2) &&
7604 p1.has_sametype(p2) && p1.has_sametype(p3));
7605
7606 if (!passed) {
7607 std::cout << " [FAIL] : has_same methods failed";
7608 throw std::runtime_error("np_test_has_same_methods failed");
7609 }
7610
7611 std::cout << " -> tests passed" << std::endl;
7612 }
has_samedomain (np_test_5_all.cpp:7603)
7593 }
7594
7595 void np_test_has_same_methods() {
7596 std::cout << "========= has_same* methods =======================";
7597
7598 numpy::Polynomial<double> p1({ 1.0, 2.0 });
7599 numpy::Polynomial<double> p2({ 1.0, 2.0 });
7600 numpy::Polynomial<double> p3({ 1.0, 3.0 });
7601
7602 bool passed = (p1.has_samecoef(p2) && !p1.has_samecoef(p3) &&
7603 p1.has_samedomain(p2) && p1.has_samewindow(p2) &&
7604 p1.has_sametype(p2) && p1.has_sametype(p3));
7605
7606 if (!passed) {
7607 std::cout << " [FAIL] : has_same methods failed";
7608 throw std::runtime_error("np_test_has_same_methods failed");
7609 }
7610
7611 std::cout << " -> tests passed" << std::endl;
7612 }
has_sametype (np_test_5_all.cpp:7604)
7594 void np_test_has_same_methods() {
7595 std::cout << "========= has_same* methods =======================";
7596
7597 numpy::Polynomial<double> p1({ 1.0, 2.0 });
7598 numpy::Polynomial<double> p2({ 1.0, 2.0 });
7599 numpy::Polynomial<double> p3({ 1.0, 3.0 });
7600
7601 bool passed = (p1.has_samecoef(p2) && !p1.has_samecoef(p3) &&
7602 p1.has_samedomain(p2) && p1.has_samewindow(p2) &&
7603 p1.has_sametype(p2) && p1.has_sametype(p3));
7604
7605 if (!passed) {
7606 std::cout << " [FAIL] : has_same methods failed";
7607 throw std::runtime_error("np_test_has_same_methods failed");
7608 }
7609
7610 std::cout << " -> tests passed" << std::endl;
7611 }
7612
7613 void np_test_trim_public() {
has_samewindow (np_test_5_all.cpp:7603)
7593 }
7594
7595 void np_test_has_same_methods() {
7596 std::cout << "========= has_same* methods =======================";
7597
7598 numpy::Polynomial<double> p1({ 1.0, 2.0 });
7599 numpy::Polynomial<double> p2({ 1.0, 2.0 });
7600 numpy::Polynomial<double> p3({ 1.0, 3.0 });
7601
7602 bool passed = (p1.has_samecoef(p2) && !p1.has_samecoef(p3) &&
7603 p1.has_samedomain(p2) && p1.has_samewindow(p2) &&
7604 p1.has_sametype(p2) && p1.has_sametype(p3));
7605
7606 if (!passed) {
7607 std::cout << " [FAIL] : has_same methods failed";
7608 throw std::runtime_error("np_test_has_same_methods failed");
7609 }
7610
7611 std::cout << " -> tests passed" << std::endl;
7612 }
integral (np_test_1_all.cpp:10913)
10903 // Si(π/2) ≈ 1.851937 (approximately)
10904 double si_pi2 = si(M_PI / 2.0);
10905 if (!(!std::isnan(si_pi2) && std::isfinite(si_pi2))) {
10906 std::string description = std::string("testTrigonometricIntegrals():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!std::isnan(si_pi2) && std::isfinite(si_pi2))";
10907 std::cout << std::string("[FAIL] ") + description << std::endl;
10908 throw std::runtime_error(description);
10909 }
10910 // std::cout << "[OK] Sine integral basic functionality\n";
10911
10912 // Test cosine integral (undefined at x = 0)
10913 if (!(std::isnan(ci(0.0)))) {
10914 std::string description = std::string("testTrigonometricIntegrals():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::isnan(ci(0.0)))";
10915 std::cout << std::string("[FAIL] ") + description << std::endl;
10916 throw std::runtime_error(description);
10917 }
10918
10919 double ci_1 = ci(1.0);
10920 if (!(!std::isnan(ci_1) && std::isfinite(ci_1))) {
10921 std::string description = std::string("testTrigonometricIntegrals():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!std::isnan(ci_1) && std::isfinite(ci_1))";
10922 std::cout << std::string("[FAIL] ") + description << std::endl;
pow (np_test_1_all.cpp:15230)
15220 std::cout << "[FAIL] Broadcasting validation failed" << std::endl;
15221 errors++;
15222 }
15223
15224 // Test 11: Performance with caching
15225 std::cout << "========= test_caching_performance =======================" ;
15226
15227 int call_count = 0;
15228 auto expensive_func = [&call_count](double x) {
15229 call_count++;
15230 return std::pow(x, 3) + std::sin(x); // Simulate expensive computation
15231 };
15232
15233 auto cached_vectorized = numpy::vectorize(expensive_func, true, 100);
15234
15235 numpy::NDArray<double> test_array({ 3 });
15236 test_array.setElementAt({ 0 }, 1.0);
15237 test_array.setElementAt({ 1 }, 2.0);
15238 test_array.setElementAt({ 2 }, 3.0);
15239
15240 // First call
roots (np_test_1_all.cpp:23043)
23033 // std::cout << "[OK] Polynomial fitting works correctly" << std::endl;
23034 std::cout << " -> tests passed" << std::endl;
23035 }
23036
23037 void test_polynomial_roots() {
23038 std::cout << "========= test_polynomial_roots =======================";
23039
23040 // Test quadratic: x^2 - 1 = (x-1)(x+1), roots should be ±1
23041 numpy::Polynomial<double> quad({ -1, 0, 1 });
23042 auto roots = quad.roots();
23043
23044 // TODO: Fix root finding algorithm - temporarily disabled
23045 // assert(roots.size() == 2);
23046
23047 // Sort roots by real part
23048 std::sort(roots.begin(), roots.end(),
23049 [](const std::complex<double>& a, const std::complex<double>& b) {
23050 return a.real() < b.real();
23051 });
set_default_printstyle (np_test_5_all.cpp:8798)
8788 std::cout << " -> tests passed" << std::endl;
8789 }
8790
8791 // ===========================
8792 // Polynomial Tests
8793 // ===========================
8794
8795 void np_test_toString_polynomial_basic() {
8796 std::cout << "========= Polynomial toString() basic ============================";
8797
8798 numpy::set_default_printstyle("ascii");
8799 numpy::set_polynomial_precision(2);
8800
8801 numpy::Polynomial<double> p({ 1.0, 2.0, 3.0 });
8802 std::string result = p.toString();
8803
8804 // std::cout << "Polynomial: " << result << std::endl;
8805
8806 verify_not_empty(result, "Polynomial toString()");
8807
8808 if (!verify_contains(result, "x", "Polynomial")) {
size (np_test_1_all.cpp:47)
37using namespace numpy;
38using namespace numpy::benchmark;
39using namespace numpy::char_;
40
41// Helper functions for array comparison tests
42namespace {
43 const double TOLERANCE = 1e-10;
44
45 bool allTrue(const NDArray<bool>& arr) {
46 std::vector<size_t> indices(arr.getShape().size(), 0);
47 do {
48 if (!arr.getElementAt(indices)) {
49 return false;
50 }
51 } while (incrementIndices(indices, arr.getShape()));
52 return true;
53 }
54
55 bool allFalse(const NDArray<bool>& arr) {
56 std::vector<size_t> indices(arr.getShape().size(), 0);
toString (np_test_1_all.cpp:6826)
6816void testDatetime64CreationDateTime() {
6817 std::cout << "========= testDatetime64CreationDateTime =======================";
6818
6819 // Test default constructor
6820 datetime64 default_dt;
6821 if (!(default_dt.isNaT())) {
6822 std::string description = std::string("testDatetime64CreationDateTime():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(default_dt.isNaT())";
6823 std::cout << std::string("[FAIL] ") + description << std::endl;
6824 throw std::runtime_error(description);
6825 }
6826 // std::cout << "Default datetime64 is NaT: " << default_dt.toString() << std::endl;
6827
6828 // Test value constructor
6829 datetime64 epoch_day(0, DateTimeUnit::Day);
6830 // std::cout << "Epoch day: " << epoch_day.toString() << std::endl;
6831
6832 // Test string constructor
6833 datetime64 date_from_string("2024-01-15");
6834 // std::cout << "Date from string '2024-01-15': " << date_from_string.toString() << std::endl;
6835
6836 datetime64 datetime_from_string("2024-01-15T10:30:45");
window (np_test_5_all.cpp:7642)
7632 std::cout << "========= cast: convert polynomial type =======================";
7633
7634 numpy::Polynomial<double> p({ 1.0, 2.0, 3.0 });
7635 std::array<double, 2> new_domain = { 0.0, 5.0 };
7636 std::array<double, 2> new_window = { -2.0, 2.0 };
7637
7638 auto casted = p.cast(new_domain, new_window);
7639
7640 bool passed = (std::abs(casted.domain()[0] - new_domain[0]) < 1e-10 &&
7641 std::abs(casted.domain()[1] - new_domain[1]) < 1e-10 &&
7642 std::abs(casted.window()[0] - new_window[0]) < 1e-10 &&
7643 std::abs(casted.window()[1] - new_window[1]) < 1e-10);
7644
7645 if (!passed) {
7646 std::cout << " [FAIL] : cast failed";
7647 throw std::runtime_error("np_test_cast failed");
7648 }
7649
7650 std::cout << " -> tests passed" << std::endl;
7651 }