Package: apollo-ad
cs107-FinalProject, Group 16Group membersEmailConnor Capitoloconnorcapitolo@g.harvard.eduKexin Huangkexinhuang@hsph.harvard.eduHaoxin Lihaoxin_li@hsph.harvard.eduChen Lucy Zhangchz522@g.harvard.eduInstallation (we assume you are familiar with virtual environments and Git)To install from pip:pipinstallapollo_adTo install from source:# install virtualenvpipinstallvirtualenv# create virtual environmentvirtualenvapollo_ad_env# activate virtual environmentsourceapollo_ad_env/bin/activate# clone from GitHubgitclonehttps://github.com/West-Coast-Quaranteam/cs107-FinalProject.git# get into the foldercdcs107-FinalProject# install requirementspipinstall-rrequirements.txtRunning Python Interactive Session From Command Line (we assume you have Python properly installed)From your command line, please run a command similar to the one below to access the Python interactive session:pythonIf done correctly, this is the prompt you should now see:>>>You're ready to use Apollo AD!Elemental Functions and ExamplesHere is a non-exhaustive list of some of the operations available in Apollo AD, and how to use them:Elemental FunctionExampleAdditionx + yMultiplicationx * ySubtractionx - yDivisionx / yNegation-xPowerx ** 2Loglog(x)Square rootsqrt(x)Exponentialexp(x)Tangenttan(x)Cosinecos(x)Sinesin(x)Inverse Tangentarctan(x)Inverse Cosinearccos(x)Inverse Sinarcsin(x)Hyperbolic Tangenttanh(x)Hyperbolic Cosinecosh(x)Hyperbolic Sinsinh(x)We've also implemented the basic comparison operators for you to use.ExamplesUIWe also provide a nice interface, where you can specify the variable values and functions without any coding:fromapollo_adimportUIUI()Click here for an UI interaction example!Welcome to Apollo AD Library!
Enter the number of variables:
2
Enter the number of functions:
3
Type the variable name of variable No. 1 (Please only input a name that CANNOT be cast as an integer or float): 
a
Type the value of variable a (Please only input a float):
3
Type the derivative seed of variable a (Please only input a float; your default input should be 1): 
1
Type the variable name of variable No. 2 (Please only input a name that CANNOT be cast as an integer or float): 
b
Type the value of variable b (Please only input a float):
2
Type the derivative seed of variable b (Please only input a float; your default input should be 1): 
1
Type function No. 1 :
a + b + sin(b)
Type function No. 2 :
sqrt(a) + log(b)
Type function No. 3 :
exp(a * b) + a ** 2
---- Summary ----
Variable(s):
{'a': '3', 'b': '2'}
Function(s): 
a + b + sin(b)
sqrt(a) + log(b)
exp(a * b) + a ** 2
---- Computing Gradients ----
# of variables < # of functions ====> automatically use the forward mode!
---- Output ----
-- Values -- 
Function F1: 5.909297426825682
Function F2: 2.4251979881288226
Function F3: 412.4287934927351
-- Gradients -- 
Function F1: [1.         0.58385316]
Function F2: [0.28867513 0.5       ]
Function F3: [ 812.85758699 1210.28638048]Programming Usageapollo_adexpects two inputs: a dictionary with variable name as the key and variable value as the value, as well as a list of strings where each string describes a function:fromapollo_adimportauto_diffvar={'x':0.5,'y':4}fct=['cos(x) + y ** 2','2 * log(y) - sqrt(x)/3','sqrt(x)/3','3 * sinh(x) - 4 * arcsin(x) + 5']out=auto_diff(var,fct)print(out)# -- Values --# Function F1: 16.87758256189037# Function F2: 2.5368864618442655# Function F3: 0.23570226039551587# Function F4: 4.468890814088047# -- Gradients --# Function F1: [-0.47942554  8.        ]# Function F2: [-0.23570226  0.5       ]# Function F3: [0.23570226 0.        ]# Function F4: [-1.23592426 -4.61880215]apollo_adsupports both forward and reverse mode in the backend, where theauto_diffclass automatically detects which is the best way to use, depending on the number of inputs and outputs. You can also directly use theForwardandReversemode class. Remember that both forward and reverse mode should produce the same results.fromapollo_adimportForward,Reversevar={'x':3,'y':4}fct=['cos(x) + y ** 2','2 * log(y) - sqrt(x)/3']out=Forward(var,fct)print(out)# -- Values --# Function F1: 15.010007503399555# Function F2: 3.2938507417182654# -- Gradients --# Function F1: [-0.14112001  8.        ]# Function F2: [0.23710829 0.5       ]var={'x':1,'y':2}fct=['x * y + exp(x * y)','x + 3 * y']out=Reverse(var,fct)print(out)# -- Values --# Function F1: 9.38905609893065# Function F2: 7.0# -- Gradients --# Function F1: [16.7781122  8.3890561]# Function F2: [1. 3.]You can also specify the seed. As the seed in forward mode is for each variable whereas in reverse mode it's for each function, we expect a dictionary for forward mode and a list for reverse mode. Here is an example:var={'x':3,'y':4}seed={'x':1,'y':2}fct=['cos(x) + y ** 2','2 * log(y) - sqrt(x)/3']z=auto_diff(var,fct,seed)# -- Values --# Function F1: 15.010007503399555# Function F2: 2.1952384530501554# -- Gradients --# Function F1: [-0.14112001 16.        ]# Function F2: [-0.09622504  1.        ]You can also run the above examples by typing:fromapollo_adimportdemodemo()Additional NotesIf you are trying to access the attributes of this package, please note that they will be lists, numpy arrays, or dictionaries.Most importantly, have fun and provide us feedback!