Programming Articles

Page 129 of 2547

Compute the natural logarithm with scimath in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 192 Views

To compute the natural logarithm with scimath, use the np.emath.log() method in Python NumPy. This function handles complex results automatically, making it safer than the standard np.log() for negative values. Syntax np.emath.log(x) Parameters: x − The value(s) whose natural logarithm is required. Can be scalar or array. Returns: The natural logarithm of x. For negative values, returns complex numbers with imaginary parts. Example with Mixed Values Let's compute the natural logarithm for various types of values including infinity and negative numbers − import numpy as np # ...

Read More

Compute the square root of negative input with emath in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 320 Views

NumPy's emath.sqrt() function computes the square root of input values, handling negative numbers by returning complex values. Unlike the standard numpy.sqrt(), which raises an error for negative inputs, emath.sqrt() returns complex numbers for negative values. Syntax numpy.emath.sqrt(x) Parameters: x − Input array or scalar value Returns: Square root of x. For negative values, returns complex numbers with imaginary components. Example with Mixed Positive and Negative Values Let's compute square roots for an array containing both positive and negative numbers − import numpy as np # Creating a ...

Read More

Return the real part of the complex argument in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 351 Views

To return the real part of complex numbers in Python, use the numpy.real() method. This function extracts the real component from complex arguments. If the input contains real numbers, it returns them with their original type. For complex arrays, it returns float values. Syntax numpy.real(val) Parameters: val − Input array containing complex numbers Basic Example Let's extract real parts from a complex array ? import numpy as np # Create complex array arr = np.array([56.+0.j, 27.+0.j, 68.+0.j, 23.+0.j]) print("Original Array:", arr) # Extract real parts real_parts = ...

Read More

Compute the square root of input with emath in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 224 Views

The numpy.emath.sqrt() function computes the square root of input values, returning complex numbers for negative inputs instead of raising errors. This function is part of NumPy's mathematical functions with automatic domain handling. Syntax numpy.emath.sqrt(x) Parameters: x − Input value (scalar or array-like). Can be positive, negative, or complex numbers. Returns: Square root of x. For negative inputs, returns complex numbers. Basic Example with Positive Numbers Let's compute square roots of positive numbers ? import numpy as np # Creating a numpy array with positive numbers arr = ...

Read More

Return real parts if input is complex with all imaginary parts close to zero in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 277 Views

The numpy.real_if_close() function returns the real parts of a complex array when the imaginary parts are close to zero. "Close to zero" is defined as tol * machine_epsilon, where tol is the tolerance parameter. Syntax numpy.real_if_close(a, tol=100) Parameters The function accepts the following parameters − a − Input array (complex or real) tol − Tolerance in machine epsilons (default: 100) Example Let's see how to extract real parts when imaginary parts are negligible − import numpy as np # Creating a complex array with very small ...

Read More

Replace NaN with zero and fill positive infinity for complex input values in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 290 Views

The numpy.nan_to_num() function replaces NaN values with zero and infinity values with large finite numbers. This is particularly useful when working with complex numbers that contain non-finite values. Syntax numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None) Parameters The function accepts the following parameters − x − Input data (array-like) copy − Whether to create a copy (True) or replace in-place (False). Default is True nan − Value to replace NaN. Default is 0.0 posinf − Value to replace positive infinity. Default is very large finite number neginf − Value to replace negative infinity. Default ...

Read More

Replace NaN with zero and fill negative infinity values in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 646 Views

In NumPy, you can handle NaN and infinity values using the numpy.nan_to_num() method. This function replaces NaN with zero and infinity values with large finite numbers, making your data suitable for mathematical operations. Syntax numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None) Parameters Parameter Description Default x Input array Required copy Whether to create a copy or modify in-place True nan Value to replace NaN with 0.0 posinf Value to replace positive infinity with Very large number neginf Value to replace negative infinity with Very ...

Read More

Return the discrete linear convolution of two one-dimensional sequences in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 290 Views

To return the discrete linear convolution of two one-dimensional sequences, use the numpy.convolve() method in Python NumPy. The convolution operator is often seen in signal processing, where it models the effect of a linear time-invariant system on a signal. In probability theory, the sum of two independent random variables is distributed according to the convolution of their individual distributions. Syntax numpy.convolve(a, v, mode='full') Parameters The method accepts the following parameters ? a ? First one-dimensional input array v ? Second one-dimensional input array mode ? Optional parameter with values 'full', 'valid', ...

Read More

Convert a polynomial to a Chebyshev series in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 342 Views

To convert a polynomial to a Chebyshev series, use the chebyshev.poly2cheb() method in Python NumPy. This method converts an array representing polynomial coefficients (ordered from lowest degree to highest) to an array of coefficients for the equivalent Chebyshev series. Syntax numpy.polynomial.chebyshev.poly2cheb(pol) Parameters pol − A 1-D array containing polynomial coefficients ordered from lowest to highest degree. Return Value Returns a 1-D array containing the coefficients of the equivalent Chebyshev series, ordered from lowest to highest degree. Example Let's convert a polynomial with coefficients [1, 2, 3, 4, 5] to its ...

Read More

Convert a Chebyshev series to a polynomial in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 379 Views

To convert a Chebyshev series to a polynomial, use the chebyshev.cheb2poly() method in Python NumPy. This function converts an array representing the coefficients of a Chebyshev series, ordered from lowest degree to highest, to an array of the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest to highest degree. The method returns a 1-D array containing the coefficients of the equivalent polynomial ordered from lowest order term to highest. The parameter c is a 1-D array containing the Chebyshev series coefficients, ordered from lowest order term to highest. Syntax numpy.polynomial.chebyshev.cheb2poly(c) ...

Read More
Showing 1281–1290 of 25,469 articles
« Prev 1 127 128 129 130 131 2547 Next »
Advertisements