Coverage for chebpy/api.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-07 10:30 +0000

1"""User-facing functions for creating and manipulating Chebfun objects. 

2 

3This module provides the main interface for users to create Chebfun objects, 

4which are the core data structure in ChebPy for representing functions. 

5""" 

6 

7from .core.bndfun import Bndfun 

8from .core.chebfun import Chebfun 

9from .core.settings import _preferences as prefs 

10from .core.utilities import Domain 

11 

12 

13def chebfun(f=None, domain=None, n=None): 

14 """Create a Chebfun object representing a function. 

15 

16 A Chebfun object represents a function using Chebyshev polynomials. This constructor 

17 can create Chebfun objects from various inputs including callable functions, 

18 constants, and special strings. 

19 

20 Args: 

21 f: The function to represent. Can be: 

22 - None: Creates an empty Chebfun 

23 - callable: A function handle like lambda x: x**2 

24 - str: A single alphabetic character (e.g., 'x') for the identity function 

25 - numeric: A constant value 

26 domain: The domain on which to define the function. Defaults to the domain 

27 specified in preferences. 

28 n: Optional number of points to use in the discretization. If None, adaptive 

29 construction is used. 

30 

31 Returns: 

32 Chebfun: A Chebfun object representing the function. 

33 

34 Raises: 

35 ValueError: If unable to construct a constant function from the input. 

36 

37 Examples: 

38 >>> # Empty Chebfun 

39 >>> f = chebfun() 

40 >>> 

41 >>> # Function from a lambda 

42 >>> f = chebfun(lambda x: np.sin(x), domain=[-np.pi, np.pi]) 

43 >>> 

44 >>> # Identity function 

45 >>> x = chebfun('x') 

46 >>> 

47 >>> # Constant function 

48 >>> c = chebfun(3.14) 

49 """ 

50 # Empty via chebfun() 

51 if f is None: 

52 return Chebfun.initempty() 

53 

54 domain = domain if domain is not None else prefs.domain 

55 

56 # Callable fct in chebfun(lambda x: f(x), ... ) 

57 if hasattr(f, "__call__"): 

58 return Chebfun.initfun(f, domain, n) 

59 

60 # Identity via chebfun('x', ... ) 

61 if isinstance(f, str) and len(f) == 1 and f.isalpha(): 

62 if n: 

63 return Chebfun.initfun(lambda x: x, domain, n) 

64 else: 

65 return Chebfun.initidentity(domain) 

66 

67 try: 

68 # Constant fct via chebfun(3.14, ... ), chebfun('3.14', ... ) 

69 return Chebfun.initconst(float(f), domain) 

70 except (OverflowError, ValueError): 

71 raise ValueError(f"Unable to construct const function from {{{f}}}") 

72 

73 

74def pwc(domain=[-1, 0, 1], values=[0, 1]): 

75 """Initialize a piecewise-constant Chebfun. 

76 

77 Creates a piecewise-constant function represented as a Chebfun object. 

78 The function takes constant values on each interval defined by the domain. 

79 

80 Args: 

81 domain (list): A list of breakpoints defining the intervals. Must have 

82 length equal to len(values) + 1. Default is [-1, 0, 1]. 

83 values (list): A list of constant values for each interval. Default is [0, 1]. 

84 

85 Returns: 

86 Chebfun: A piecewise-constant Chebfun object. 

87 

88 Examples: 

89 >>> # Create a step function with value 0 on [-1,0] and 1 on [0,1] 

90 >>> f = pwc() 

91 >>> 

92 >>> # Create a custom piecewise-constant function 

93 >>> f = pwc(domain=[-2, -1, 0, 1, 2], values=[-1, 0, 1, 2]) 

94 """ 

95 funs = [] 

96 intervals = [x for x in Domain(domain).intervals] 

97 for interval, value in zip(intervals, values): 

98 funs.append(Bndfun.initconst(value, interval)) 

99 return Chebfun(funs)