Coverage for src / chebpy / api.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-20 05:55 +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 .bndfun import Bndfun 

8from .chebfun import Chebfun 

9from .settings import _preferences as prefs 

10from .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 >>> import numpy as np 

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

44 >>> 

45 >>> # Identity function 

46 >>> x = chebfun('x') 

47 >>> 

48 >>> # Constant function 

49 >>> c = chebfun(3.14) 

50 """ 

51 # Empty via chebfun() 

52 if f is None: 

53 return Chebfun.initempty() 

54 

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

56 

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

58 if hasattr(f, "__call__"): 

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

60 

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

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

63 if n: 

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

65 else: 

66 return Chebfun.initidentity(domain) 

67 

68 try: 

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

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

71 except (OverflowError, ValueError): 

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

73 

74 

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

76 """Initialize a piecewise-constant Chebfun. 

77 

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

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

80 

81 Args: 

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

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

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

85 

86 Returns: 

87 Chebfun: A piecewise-constant Chebfun object. 

88 

89 Examples: 

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

91 >>> f = pwc() 

92 >>> 

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

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

95 """ 

96 funs = [] 

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

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

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

100 return Chebfun(funs)