user defined functions - Python -- nondimensionalize -
i'm writing python module allow me make unit-based calculations, , i'm trying implement unit-sensitive integration of functions. idea write wrapper scipy.integrate -- take function , arguments given, including limits of integration, nondimensionalize them all, pass scipy.integrate.quad or such thing, answer, , multiply correct units @ end.
to accomplish this, i'm trying figure out how nondimensionalize arbitrary function. i've implemented units if divide 2 quantities same units, returns ordinary number, first thought this:
def nondimensionalize(func, *args): val = func(*args) dimensions = val / val.value return lambda args : (func(args) / dimensions)
this works charm nondimensionalize function's output, i'm having harder time input. need return function takes in ordinary numbers, multiplies them correct si dimensions (which can figure out how do), gets output, divides correct si dimensions, , returns value ordinary number. can pass said function scipy.integrate (or scipy.fslove, etc.). tried following:
def nondimensionalize(func, *args): argdims = [] arg in args: adim = arg / arg.value argdims.append(adim) ndargs = [] index = 0 arg in args: ndargs.append(arg / argdims[index]) index += 1 val = func(*args) dimensions = val / val.value return lambda args : (func(args) / dimensions)
but doesn't work; has same effect four-line function above. i'm not sure how proceed @ point. help?
what need return function takes in ordinary numbers, multiplies them correct si dimensions (which can figure out how do), gets output, divides correct si dimensions, , returns value ordinary number.
i'm not sure understand how dimensionalize/non-dimensionalize values, modify corresponding functions necessary, this:
def dimensionalizevalue(nondimvalue, dimensions): return nondimvalue * dimensions def nondimensionalizevalue(dimvalue): dimensions = dimvalue / dimvalue.value return dimvalue / dimensions def nondimensionalizefunction(function): def wrapper(*nondimargs): # figure out correct dimensions. dimensions = none # transform/dimensionalize arguments. dimargs = [dimensionalizevalue(arg, dimensions) arg in nondimargs] # output using dimensionalized arguments. dimval = function(*dimargs) # non-dimensionalize output. nondimval = nondimensionalizevalue(dimval) return nondimval return wrapper
Comments
Post a Comment