Coverage for autodiff_team29/vector_function.py: 95%

19 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-15 21:37 +0000

1from typing import List 

2 

3import numpy as np 

4from numpy.typing import NDArray 

5 

6from autodiff_team29 import Node 

7 

8 

9class VectorFunction: 

10 def __init__(self, functions: List[Node]) -> None: 

11 """ 

12 Computes forward mode automatic differentiation for provided functions 

13 

14 Parameters 

15 ---------- 

16 functions : Node or List[Node] 

17 functions that compose the vector function 

18 

19 Raises: 

20 ------ 

21 ValueError : 

22 Raise value error if functions is not List[Node] 

23 

24 Example 

25 ------- 

26 

27 Consider the function f(x,y) = [x + y, x-2y]. 

28 An instantiation would be 

29 >>> x = Node("x", 2, 1 ,seed_vector=[1,0]) 

30 >>> y = Node("y", 3, 1, seed_vector=[0,1]) 

31 >>> f = VectorFunction([x + y, x - 2 * y]) 

32 

33 """ 

34 

35 if isinstance(functions, list): 

36 assert all(isinstance(f, Node) for f in functions) 

37 self._functions = functions 

38 else: 

39 raise ValueError("functions argument must be a list of Nodes") 

40 

41 @property 

42 def symbol(self) -> str: 

43 """ 

44 Returns the symbolic representation of the vector function 

45 

46 """ 

47 return str(np.array([function.symbol for function in self._functions])) 

48 

49 @property 

50 def value(self) -> NDArray[float]: 

51 """ 

52 Returns the computed value of the vector function 

53 

54 """ 

55 return np.array([function.value for function in self._functions]) 

56 

57 @property 

58 def jacobian(self) -> NDArray[float]: 

59 """ 

60 Returns the computed Jacobian of the vector function 

61 

62 """ 

63 return np.array([function.derivative for function in self._functions])