forked from mattloper/chumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
116 lines (87 loc) · 2.59 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from ch import *
from logic import *
from optimization import minimize
import extras
import testing
from version import version as __version__
from numpy import bool, int, float, complex, object, unicode, str, nan, inf
def test():
from os.path import split
import unittest
test_loader= unittest.TestLoader()
test_loader = test_loader.discover(split(__file__)[0])
test_runner = unittest.TextTestRunner()
test_runner.run( test_loader )
demos = {}
demos['scalar'] = """
import chumpy as ch
[x1, x2, x3] = ch.array(10), ch.array(20), ch.array(30)
result = x1+x2+x3
print result # prints [ 60.]
print result.dr_wrt(x1) # prints 1
"""
demos['show_tree'] = """
import chumpy as ch
[x1, x2, x3] = ch.array(10), ch.array(20), ch.array(30)
for i in range(3): x2 = x1 + x2 + x3
x2.dr_wrt(x1) # pull cache
x2.dr_wrt(x3) # pull cache
x1.label='x1' # for clarity in show_tree()
x2.label='x2' # for clarity in show_tree()
x3.label='x3' # for clarity in show_tree()
x2.show_tree(cachelim=1e-4) # in MB
"""
demos['matrix'] = """
import chumpy as ch
x1, x2, x3, x4 = ch.eye(10), ch.array(1), ch.array(5), ch.array(10)
y = x1*(x2-x3)+x4
print y
print y.dr_wrt(x2)
"""
demos['linalg'] = """
import chumpy as ch
m = [ch.random.randn(100).reshape((10,10)) for i in range(3)]
y = m[0].dot(m[1]).dot(ch.linalg.inv(m[2])) * ch.linalg.det(m[0])
print y.shape
print y.dr_wrt(m[0]).shape
"""
demos['inheritance'] = """
import chumpy as ch
import numpy as np
class Sin(ch.Ch):
dterms = ('x',)
def compute_r(self):
return np.sin(self.x.r)
def compute_dr_wrt(self, wrt):
import scipy.sparse
if wrt is self.x:
result = np.cos(self.x.r)
return scipy.sparse.diags([result.ravel()], [0]) if len(result)>1 else np.atleast_2d(result)
x1 = Ch([10,20,30])
result = Sin(x1) # or "result = Sin(x=x1)"
print result.r
print result.dr_wrt(x1)
"""
demos['optimization'] = """
import chumpy as ch
x = ch.zeros(10)
y = ch.zeros(10)
# Beale's function
e1 = 1.5 - x + x*y
e2 = 2.25 - x + x*(y**2)
e3 = 2.625 - x + x*(y**3)
objective = {'e1': e1, 'e2': e2, 'e3': e3}
ch.minimize(objective, x0=[x,y], method='dogleg')
print x # should be all 3.0
print y # should be all 0.5
"""
def demo(which=None):
if which not in demos:
print 'Please indicate which demo you want, as follows:'
for key in demos:
print "\tdemo('%s')" % (key,)
return
print '- - - - - - - - - - - <CODE> - - - - - - - - - - - -'
print demos[which]
print '- - - - - - - - - - - </CODE> - - - - - - - - - - - -\n'
exec('global np\n' + demos[which], globals(), locals())