Build a scalar expression graph from two ordered lists of dictionaries. Each leaf has id and data. Each operation has id, op, left, and right; left and right name previously created nodes, and op is "+" or "*".
z=x+y z=xyHere x and y are the left and right parent values, and z is the result of the selected operation.
Each returned node has exactly id, data, grad, op, and parents. Its data is the supplied leaf value or the operation result as a Python float, and grad is 0.0. Leaves have an empty op string and an empty parents list. Operation nodes retain their symbol and a list of left and right parent IDs, in that order.
Return a tuple containing the list of fresh node dictionaries in creation order, followed by the final node ID as a string. Leaves come first in their input order, then operations; with no operations, the final node is the last leaf.
Input: leaves=[{'id':'a','data':2.0},{'id':'b','data':-3.0},{'id':'c','data':10.0},{'id':'f','data':-2.0}], operations=[{'id':'e','op':'*','left':'a','right':'b'},{'id':'d','op':'+','left':'e','right':'c'},{'id':'L','op':'*','left':'d','right':'f'}]
Output: ([{'id':'a','data':2.0,'grad':0.0,'op':'','parents':[]},{'id':'b','data':-3.0,'grad':0.0,'op':'','parents':[]},{'id':'c','data':10.0,'grad':0.0,'op':'','parents':[]},{'id':'f','data':-2.0,'grad':0.0,'op':'','parents':[]},{'id':'e','data':-6.0,'grad':0.0,'op':'*','parents':['a','b']},{'id':'d','data':4.0,'grad':0.0,'op':'+','parents':['e','c']},{'id':'L','data':-8.0,'grad':0.0,'op':'*','parents':['d','f']}],'L')
Explanation: Each operation reads previously created values and records its ordered parent IDs.
Input: leaves=[{'id':'x','data':1.5},{'id':'y','data':2.5}], operations=[{'id':'z','op':'+','left':'x','right':'y'}]
Output: ([{'id':'x','data':1.5,'grad':0.0,'op':'','parents':[]},{'id':'y','data':2.5,'grad':0.0,'op':'','parents':[]},{'id':'z','data':4.0,'grad':0.0,'op':'+','parents':['x','y']}],'z')
Input: leaves=[{'id':'only','data':-7.0}], operations=[]
Output: ([{'id':'only','data':-7.0,'grad':0.0,'op':'','parents':[]}],'only')
A dictionary keyed by node ID makes parent values easy to retrieve.
Appending nodes to a separate list preserves creation order.
Accepts: array
Accepts: array
Build a scalar expression graph from two ordered lists of dictionaries. Each leaf has id and data. Each operation has id, op, left, and right; left and right name previously created nodes, and op is "+" or "*".
z=x+y z=xyHere x and y are the left and right parent values, and z is the result of the selected operation.
Each returned node has exactly id, data, grad, op, and parents. Its data is the supplied leaf value or the operation result as a Python float, and grad is 0.0. Leaves have an empty op string and an empty parents list. Operation nodes retain their symbol and a list of left and right parent IDs, in that order.
Return a tuple containing the list of fresh node dictionaries in creation order, followed by the final node ID as a string. Leaves come first in their input order, then operations; with no operations, the final node is the last leaf.
Input: leaves=[{'id':'a','data':2.0},{'id':'b','data':-3.0},{'id':'c','data':10.0},{'id':'f','data':-2.0}], operations=[{'id':'e','op':'*','left':'a','right':'b'},{'id':'d','op':'+','left':'e','right':'c'},{'id':'L','op':'*','left':'d','right':'f'}]
Output: ([{'id':'a','data':2.0,'grad':0.0,'op':'','parents':[]},{'id':'b','data':-3.0,'grad':0.0,'op':'','parents':[]},{'id':'c','data':10.0,'grad':0.0,'op':'','parents':[]},{'id':'f','data':-2.0,'grad':0.0,'op':'','parents':[]},{'id':'e','data':-6.0,'grad':0.0,'op':'*','parents':['a','b']},{'id':'d','data':4.0,'grad':0.0,'op':'+','parents':['e','c']},{'id':'L','data':-8.0,'grad':0.0,'op':'*','parents':['d','f']}],'L')
Explanation: Each operation reads previously created values and records its ordered parent IDs.
Input: leaves=[{'id':'x','data':1.5},{'id':'y','data':2.5}], operations=[{'id':'z','op':'+','left':'x','right':'y'}]
Output: ([{'id':'x','data':1.5,'grad':0.0,'op':'','parents':[]},{'id':'y','data':2.5,'grad':0.0,'op':'','parents':[]},{'id':'z','data':4.0,'grad':0.0,'op':'+','parents':['x','y']}],'z')
Input: leaves=[{'id':'only','data':-7.0}], operations=[]
Output: ([{'id':'only','data':-7.0,'grad':0.0,'op':'','parents':[]}],'only')
A dictionary keyed by node ID makes parent values easy to retrieve.
Appending nodes to a separate list preserves creation order.
Accepts: array
Accepts: array