LegendrePolynomial#

class numpy::LegendrePolynomial#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Use LegendrePolynomial
LegendrePolynomial obj;
// ... operations ...

Constructors#

Signature

Location

Example

LegendrePolynomial(const std::vector<T>&coeffs = {T(0)})

NP_SPECIAL_POLYNOMIALS.H:255

Indexing / Selection#

Signature

Return Type

Location

Example

std::string get_default_printstyle()

std::string

NP_SPECIAL_POLYNOMIALS.H:325

View

PolynomialPrintOptions get_printoptions()

PolynomialPrintOptions

NP_SPECIAL_POLYNOMIALS.H:333

View

Other Methods#

Signature

Return Type

Location

Example

TargetType convert()

TargetType

NP_SPECIAL_POLYNOMIALS.H:339

View

std::pair<NDArray<T>,NDArray<T>> gauss_quadrature(size_tn)

std::pair<NDArray<T>,NDArray<T>>

NP_SPECIAL_POLYNOMIALS.H:296

LegendrePolynomial<T> generate(size_tn)

LegendrePolynomial<T>

NP_SPECIAL_POLYNOMIALS.H:259

View

void set_default_printstyle(const std::string &style)

void

NP_SPECIAL_POLYNOMIALS.H:321

View

void set_precision(intprecision)

void

NP_SPECIAL_POLYNOMIALS.H:329

std::string toString_ascii()

std::string

NP_SPECIAL_POLYNOMIALS.H:345

std::string toString_unicode()

std::string

NP_SPECIAL_POLYNOMIALS.H:386

Code Examples#

The following examples are extracted from the test suite.

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 "
convert (np_test_5_all.cpp:9049)
9039      throw std::runtime_error("Static method and global function don't share state");
9040    }
9041
9042    // Reset to default ASCII
9043    numpy::set_default_printstyle("ascii");
9044
9045    std::cout << " -> tests passed" << std::endl;
9046  }
9047
9048  void np_test_toString_polynomial_convert() {
9049    std::cout << "========= polynomial.convert(): basis conversion (NumPy API) =======================";
9050
9051    // Test Chebyshev -> Standard conversion (user's example)
9052    // NumPy: numpy.polynomial.Chebyshev([1,2,3]).convert(kind=np.polynomial.Polynomial)
9053    // Expected: -2.0 + 2.0 x + 6.0 x**2
9054    {
9055      numpy::special::ChebyshevPolynomial<double> cheb({ 1.0, 2.0, 3.0 });
9056      auto std_poly = cheb.convert<numpy::Polynomial<double>>();
9057
9058      if (std_poly.coefficients().size() != 3) {
9059        throw std::runtime_error("Chebyshev conversion: wrong coefficient count");
generate (np_test_1_all.cpp:6129)
6119    std::cout << " -> tests passed" << std::endl;
6120}
6121
6122void test_data_generator_randomBenchmarkSorting() {
6123    std::cout << "========= test_data_generator_random =======================";
6124
6125    DataGenerator<int> gen(42); // Fixed seed for reproducibility
6126
6127    // Test random data generation
6128    std::vector<int> data = gen.generate(100, DataPattern::RANDOM);
6129
6130    if (!(data.size() == 100)) {
6131        std::string description = std::string("test_data_generator_randomBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 100)";
6132        std::cout << std::string("[FAIL] ") + description << std::endl;
6133        throw std::runtime_error(description);
6134    }
6135
6136    // Check that not all elements are the same (should be random)
6137    bool has_variation = false;
6138    for (size_t i = 1; i < data.size(); ++i) {
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")) {