This is because you cannot use and on numpy arrays. You need to replace the and with * and the or with + for numpy boolean arrays. (and do not forget to add parentheses).
NumPy
numpy.org › doc › stable › reference › generated › numpy.piecewise.html
numpy.piecewise — NumPy v2.4 Manual
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Try it in your browser! ... Define the signum function, which is -1 for x < 0 and +1 for x >= 0. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.])
w3resource
w3resource.com › numpy › functional-programming › piecewise.php
NumPy Functional programming: piecewise() function - w3resource
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Define the sigma function, which is -1 for x < 0 and +1 for x >= 0. >>> import numpy as np >>> x = np.linspace(-3.5, 3.5, 5) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1])
Top answer 1 of 2
6
This is because you cannot use and on numpy arrays. You need to replace the and with * and the or with + for numpy boolean arrays. (and do not forget to add parentheses).
2 of 2
4
Another problem, to add to Nicolas's answer, is that each element of funclist must be callable if you want to use piecewise. Your corrected code would look like
t = np.arange(-2,2,.01)
f1 = lambda t: (t+2)**3
f2 = lambda t: (-t)**3
f3 = lambda t: (t)**3
f4 = lambda t: -(2-t)**3
fx = np.piecewise(t, [(t< -1)*(t>=-2), (t <= 0) * (t>=-1), (t>0) * (t<1),(t>=1) * (t<=2)], [f1,f2,f3,f4])
plot(t,fx)
Instead, you could use select
t = np.arange(-2,2,.01)
f = lambda x: x**3
fx = np.select([(t< -1)*(t>=-2), (t <= 0) * (t>=-1), (t>0) * (t<1),(t>=1) * (t<=2)], [f(t+2),f(-t),f(t),-f(2-t)])
plot(t,fx)
Moreover, select allows you to set a default value outside the defined intervals, by passing it into the parameter default. You may need that if you want to stick to a range (-10,10) with your intervals.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.piecewise.html
numpy.piecewise — NumPy v2.2 Manual
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Define the signum function, which is -1 for x < 0 and +1 for x >= 0. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.])
Sketchy thoughts
sfalsharif.wordpress.com › 2009 › 12 › 18 › piecwise-function-definitions-in-numpy
Piecewise function definitions in NumPy | Sketchy thoughts
March 28, 2010 - The basic format of the piecewise statement, ignoring optional arguments, is numpy.piecewise(x, condlist, funclist) , where x is a NumPy ndarray, condlist is the list of boolean arrays upon which the piecwise function defenition depends, and funclist is a list of functions corresponding to each of the boolean conditons.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.piecewise.html
numpy.piecewise — NumPy v2.1 Manual
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Define the signum function, which is -1 for x < 0 and +1 for x >= 0. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.])
NumPy
numpy.org › doc › 1.23 › reference › generated › numpy.piecewise.html
numpy.piecewise — NumPy v1.23 Manual
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Define the sigma function, which is -1 for x < 0 and +1 for x >= 0. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.])
GitHub
github.com › numpy › numpy › pull › 5187
Enhance piecewise to allow functions to have arbitrary number of input arguments by pbrod · Pull Request #5187 · numpy/numpy
Made piecewise function simpler and more general. The new piecewise function allows functions of the type f(x0, x1,.. ,xn) i.e. to have arbitrary number of input arguments that are evaluated condit...
Author numpy
ProgramCreek
programcreek.com › python › example › 102171 › numpy.piecewise
Python Examples of numpy.piecewise
GCP: ratings from https://cloud.google.com/compute/docs/disks/performance#ssd-pd-performance AWS: to achieve good performance on EBS, one needs to use an EBS-optimized VM instance, and the smallest VM instance that can be EBS optimized is *.large VM types (e.g., c4.large), those comes with 2 cores. ratings from http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html """ if self._provider == GCP: value = self._iops self._cpu_count = int( numpy.piecewise([value], [[value <= 15000], [ (value > 15000) & (value <= 25000) ], [value > 25000]], [lambda x: 1, lambda x: 16, lambda x: 32])) elif self._provider == AWS: self._cpu_count = 2
NumPy
numpy.org › doc › 1.19 › reference › generated › numpy.piecewise.html
numpy.piecewise — NumPy v1.19 Manual
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Define the sigma function, which is -1 for x < 0 and +1 for x >= 0. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.])
University of Texas at Austin
het.as.utexas.edu › HET › Software › Numpy › reference › generated › numpy.piecewise.html
numpy.piecewise — NumPy v1.9 Manual
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Define the sigma function, which is -1 for x < 0 and +1 for x >= 0. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.])
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.piecewise.html
numpy.piecewise — NumPy v2.3 Manual
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Try it in your browser! ... Define the signum function, which is -1 for x < 0 and +1 for x >= 0. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.])
Mabouali
mabouali.com › 2024 › 02 › 04 › piecewise-linear-functions-part-i
Piecewise-Linear Functions: Part I – Moe’s Homepage
If the first condition is true, the first function from the list is returned; If the second condition is true, the second function is returned. ... import numpy as np def my_piecewise_function_2(x): return np.piecewise( x, # The value at which the piecewise function should be evaluated [ # ...
JAX Documentation
docs.jax.dev › en › latest › _autosummary › jax.numpy.piecewise.html
jax.numpy.piecewise — JAX documentation
An array which is the result of evaluating the functions on x at the specified conditions. ... Here’s an example of a function which is zero for negative values, and linear for positive values: >>> x = jnp.array([-4, -3, -2, -1, 0, 1, 2, 3, 4]) >>> condlist = [x < 0, x >= 0] >>> funclist = [lambda x: 0 * x, lambda x: x] >>> jnp.piecewise(x, condlist, funclist) Array([0, 0, 0, 0, 0, 1, 2, 3, 4], dtype=int32)
SciPy
docs.scipy.org › doc › › numpy-1.8.1 › reference › generated › numpy.piecewise.html
numpy.piecewise — NumPy v1.8 Manual
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Define the sigma function, which is -1 for x < 0 and +1 for x >= 0. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.])
NumPy
numpy.org › devdocs › reference › generated › numpy.piecewise.html
numpy.piecewise — NumPy v2.5.dev0 Manual
This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from condlist. ... |-- |funclist[0](x[condlist[0]]) out = |funclist[1](x[condlist[1]]) |... |funclist[n2](x[condlist[n2]]) |-- ... Try it in your browser! ... Define the signum function, which is -1 for x < 0 and +1 for x >= 0. >>> x = np.linspace(-2.5, 2.5, 6) >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1]) array([-1., -1., -1., 1., 1., 1.])