Code Documentation¶
Documentation for FormulaLab.search module. This is the core module that aims to provide a search engine for formulas database.
-
class
FormulaLab.search.
FormulaSearch
(data, formula_col='Formula', id_col='ID', save_all_derived_formulas=True)¶ FormulaSearch is a class that search in formulas database. It can go through equations to find connections between two differernt variables in different formulas. The database should be a DataFrame, list, tuple, set, or dict.
- The main functionalties for FormulaSearch are:
- 1- find(function)
Directly search for formulas.
- 2- derive(function (variable))
Derive all possible functions(variable).
- Parameters
- dataPandas.DataFrame, list, dict, set or tuple
Your formulas database.
- formula_colstr, optional
The column name of formulas database if the input data is a DataFrame. The formula column name must match formula_col. The default is “Formula”.
- id_colstr, optional
The ID column of the database must match the id_col. The default is ‘ID’.
- save_all_derived_formulasbool
All derieved formulas are temporarily stored in ‘self.all_derived_formulas’
- Returns
- obj
Examples
>>> import FormulaLab as fl >>> data = ['f = m*a', 'a = v/t', 'd = v*t'] >>> phyfos = fl.FormulaSearch(data) >>> phyfos.data ID Formula Args 0 1 f = m*a [m, f, a] 1 2 a = v/t [v, a, t] 2 3 d = v*t [v, d, t] *The Args col. is automaticlly generated
- Attributes
- datapandas.DataFrame
- all_derived_formulasset
Collect all derived formulas that have been found by derive().
- traceslist
Once the derive method is called, one can see the traces of the derivation.
Methods
derive(func, var)
Find formulas through algebric substitutions.
find(func, vars, id)
Directly search for a function func in terms of vars or id.
function(func:symbols)
Converts a symbolic expr to a python function, wraping over sympy.
find_raw_formula(id)
Find a formula in the database based on its id, in string format.
solve_for(expr:str, var:str)
Solve for a variable from an exprestion, wraping over sympy.
trace(path)
Shows the detailed path of how to get from a function to a variable.
-
derive
(self, func: str, var: str, shortest_path=True) → list¶ Search for the func and var, and connects them algabrically.
- Parameters
- funcstr
A funciton to solve for.
- varstr
A variable to be present in the derived formula.
- Returns
- list
All found solutions by different paths.
See also
find
For direct search
Examples
>>> import FormulaLab as fl >>> data = ['f = m*a', 'a = v/t', 'v = d/t'] >>> # Say you want to know what is f(d) = ? >>> phyfos = fl.FormulaSearch(data) >>> phyfos.data ID Formula Args 0 1 f = m*a [m, f, a] 1 2 a = v/t [v, a, t] 2 3 v = d/t [v, d, t] >>> f_d = phyfos.derive('f', 'd') >>> print(f_d) [d*m/t**2, m*v**2/d]
-
find
(self, func: str, vars: list = None, id: int = None, function: bool = False) → list¶ Direct search for a function with respect to variable(s). If id is specified, then the search is limited to one formula with id = “id”. If more than one variable are introdueced, then only formula(s) with all specified variable(s) are found.
- Parameters
- funcstr
Desired function.
- varslist
Desired variables
- idint, optional
Restrict the search to one formula
- function: bool
To convert the output function to a python function
- Returns
- list
All found formulas in symbolic form. Or as a python function, at function=True
See also
derive
For indirect search
Examples
>>> import FormulaLab as fl >>> data = ['f = m*a', 'a = v/t', 'd = v*t'] >>> # Say you want to know what is v(t,a) = ? >>> phyfos = fl.FormulaSearch(data) >>> phyfos.data ID Formula Args 0 1 f = m*a [m, f, a] 1 2 a = v/t [v, a, t] 2 3 d = v*t [v, d, t] *The Args col. is automaticlly generated >>> v_t_a = phyfos.find('v', ['t','a']) >>> print(v_t_a) [a*t]
Now, say you are only focused on one formula (eg., ID=3) and you want to call it in different places in your code/project, but you do not want to rewrite it again many times. Here where find becomes handy! In any place in your project, you call you formula by its id and in any form you want: func (vars). For example,
>>> a = phyfos.find(func='a', id=1) >>> print(a) [f/m]
If you wish the output to be as a pyhon func, then:
>>> a = phyfos.find(func='a', id=1, function=True) >>> a(f=5,m=2) 2.5
-
find_raw_formula
(self, id)¶ Find a formula in the database based on its id, in string format.
- Parameters
- idint
Formula id.
- Returns
- str
The formula as it is in the database.
See also
find
In symbolic format
-
static
function
(formula: <function symbols at 0x000002023BF21708>)¶ Convert a symbolic formula to a python function Similar to find(,, function=True). A static method that is wrapped over sympy.lambdify
- Parameters
- formulasp.symbols or list
Formula(s) in symbolic form, or list of them.
- Returns
- function
python function. Or list of python functions, depends on input.
See also
find
find(,, function=True)
Examples
>>> import FormulaLab as fl >>> expr = m * a >>> # You want to convert expr into a python function >>> expr_f = fl.FormulaSearch.function(expr) >>> print(expr_f(m=2, a=3)) 6.0
-
get_formula_info
(self, var: tuple, target_col: str = 'ID') → list¶ search in target_col bases on variable(s). target_col can be any column in the database
- Parameters
- vartuple of str
The varible of interest.
- target_colstr, optional
The search col. The default is ‘ID’.
- Returns
- list
list of the found information.
-
static
solve_for
(expr: str, var: str) → list¶ solve for var in expr. This function wrap over sympy.solve
- Parameters
- exprstr
A formula in a string format.
- varstr
The variable that is being solved for.
- Returns
- list of list
list of list of exprestions of sympy.symbols.
Examples
>>> import FormulaLab as fl >>> fl.FormulaSearch.solve_for(expr="f = m * a", var='a') [[ f / m ]]
-
trace
(self, path: list) → list¶ Shows the detailed path of how to get from a function to a variable. eg., [1, ‘a’, 2, ‘b’, 3], where [1,2,3] is the path, and [‘a’,’b’] is the connected variable list.
- Parameters
- pathlist
A list of connected ides of formulas
- Returns
- list
detailed path of connected variables and formulas’ id.
Examples
>>> import FormulaLab as fl >>> data = ['f = m*a', 'a = v/t', 'd = v*t'] >>> phyfos = fl.FormulaSearch(data) >>> phyfos.data ID Formula Args 0 1 f = m*a [m, f, a] 1 2 a = v/t [v, a, t] 2 3 d = v*t [v, d, t] >>> phyfos.trace([1,2,3]) [[1, 'a', 2, 't', 3], [1, 'a', 2, 'v', 3]]