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
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-15 21:37 +0000
1from typing import List
3import numpy as np
4from numpy.typing import NDArray
6from autodiff_team29 import Node
9class VectorFunction:
10 def __init__(self, functions: List[Node]) -> None:
11 """
12 Computes forward mode automatic differentiation for provided functions
14 Parameters
15 ----------
16 functions : Node or List[Node]
17 functions that compose the vector function
19 Raises:
20 ------
21 ValueError :
22 Raise value error if functions is not List[Node]
24 Example
25 -------
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])
33 """
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")
41 @property
42 def symbol(self) -> str:
43 """
44 Returns the symbolic representation of the vector function
46 """
47 return str(np.array([function.symbol for function in self._functions]))
49 @property
50 def value(self) -> NDArray[float]:
51 """
52 Returns the computed value of the vector function
54 """
55 return np.array([function.value for function in self._functions])
57 @property
58 def jacobian(self) -> NDArray[float]:
59 """
60 Returns the computed Jacobian of the vector function
62 """
63 return np.array([function.derivative for function in self._functions])