示例#1
0
 def test_09_CDComposDA(self):
     da = tpsa.base()
     x1 = da[0] + 2 * da[1] + 3 * da[2]
     x2 = tpsa.sin(x1)
     x1 = tpsa.cos(x1)
     x3 = 0.5 * da[0] + 4 * da[1] + 2.7 * da[2]
     x4 = tpsa.sin(x3)
     x3 = tpsa.cos(x3)
     y1 = tpsa.complex(x1, x2)
     y2 = tpsa.complex(x3, x4)
     cmmap = tpsa.assign_cd(2)
     cmmap[0] = tpsa.complex(x1, tpsa.exp(x1))
     cmmap[1] = tpsa.complex(x2, tpsa.exp(x2))
     mmap = tpsa.assign(3)
     mmap[0] = x1
     mmap[1] = x2
     mmap[2] = x1 + 0.33 * x2
     comap = tpsa.assign_cd(2)
     tpsa.cd_composition(cmmap, mmap, comap)
     self.assertTrue(
         tpsa.compare_cd_with_file(self.path + "cd_composition_da_0.txt",
                                   comap[0], self.eps))
     self.assertTrue(
         tpsa.compare_cd_with_file(self.path + "cd_composition_da_1.txt",
                                   comap[1], self.eps))
示例#2
0
 def test_08_DAComposCD(self):
     da = tpsa.base()
     x1 = da[0] + 2 * da[1] + 3 * da[2]
     x2 = tpsa.sin(x1)
     x1 = tpsa.cos(x1)
     x3 = 0.5 * da[0] + 4 * da[1] + 2.7 * da[2]
     x4 = tpsa.sin(x3)
     x3 = tpsa.cos(x3)
     y1 = tpsa.complex(x1, x2)
     y2 = tpsa.complex(x3, x4)
     mmap = tpsa.assign(2)
     mmap[0] = x1
     mmap[1] = x2
     cnmap = tpsa.assign_cd(3)
     cnmap[0] = y1
     cnmap[1] = y2
     cnmap[2] = y1 * y2
     comap = tpsa.assign_cd(2)
     tpsa.cd_composition(mmap, cnmap, comap)
     self.assertTrue(
         tpsa.compare_cd_with_file(self.path + "da_composition_cd_0.txt",
                                   comap[0], self.eps))
     self.assertTrue(
         tpsa.compare_cd_with_file(self.path + "da_composition_cd_1.txt",
                                   comap[1], self.eps))
示例#3
0
 def test_07_CDfuncs(self):
     da = tpsa.base()
     x1 = da[0] + 2 * da[1] + 3 * da[2]
     x2 = tpsa.sin(x1)
     x1 = tpsa.cos(x1)
     x3 = 0.5 * da[0] + 4 * da[1] + 2.7 * da[2]
     x4 = tpsa.sin(x3)
     x3 = tpsa.cos(x3)
     y1 = tpsa.complex(x1, x2)
     y2 = tpsa.complex(x3, x4)
     r = y1 + y2
     self.assertTrue(
         tpsa.compare_cd_with_file(self.path + "cd_calculation_0.txt", r,
                                   self.eps))
     r = y1 - y2
     self.assertTrue(
         tpsa.compare_cd_with_file(self.path + "cd_calculation_1.txt", r,
                                   self.eps))
     r = y1 * y2
     self.assertTrue(
         tpsa.compare_cd_with_file(self.path + "cd_calculation_2.txt", r,
                                   self.eps))
     r = y1 / y2
     self.assertTrue(
         tpsa.compare_cd_with_file(self.path + "cd_calculation_3.txt", r,
                                   self.eps))
示例#4
0
 def test_04_SubstDAs(self):
     da = tpsa.base()
     x = 1 + da[0] + 2 * da[1] + 5 * da[2]
     y = tpsa.exp(x)
     z = tpsa.assign()
     idx = [0, 1]
     lv = tpsa.assign(2)
     lv[0] = tpsa.sin(x)
     lv[1] = tpsa.cos(x)
     tpsa.da_substitute(y, idx, lv, z)
     self.assertTrue(
         tpsa.compare_da_with_file(
             self.path + "substitute_multiple_da_vectors.txt", z, self.eps))
示例#5
0
 def test_06_Composition(self):
     da = tpsa.base()
     x = 1 + da[0] + 2 * da[1] + 5 * da[2]
     y = tpsa.exp(x)
     lx = tpsa.assign(3)
     lx[0] = x
     lx[1] = y
     lx[2] = tpsa.sinh(x)
     lu = tpsa.assign(3)
     lu[0] = tpsa.sin(x)
     lu[1] = tpsa.cos(x)
     lu[2] = tpsa.tan(x)
     ly = tpsa.assign(3)
     tpsa.da_composition(lx, lu, ly)
     self.assertTrue(
         tpsa.compare_da_with_file(self.path + "da_composition_0.txt",
                                   ly[0], self.eps))
     self.assertTrue(
         tpsa.compare_da_with_file(self.path + "da_composition_1.txt",
                                   ly[1], self.eps))
     self.assertTrue(
         tpsa.compare_da_with_file(self.path + "da_composition_2.txt",
                                   ly[2], self.eps))
示例#6
0
 def test_05_SubstBunch(self):
     da = tpsa.base()
     x = 1 + da[0] + 2 * da[1] + 5 * da[2]
     y = tpsa.exp(x)
     z = tpsa.assign()
     idx = [0, 1]
     lv = tpsa.assign(2)
     lv[0] = tpsa.sin(x)
     lv[1] = tpsa.cos(x)
     lx = tpsa.assign(3)
     lx[0] = x
     lx[1] = y
     lx[2] = tpsa.sinh(x)
     ly = tpsa.assign(3)
     tpsa.da_substitute(lx, idx, lv, ly)
     self.assertTrue(
         tpsa.compare_da_with_file(self.path + "bunch_substitution_0.txt",
                                   ly[0], self.eps))
     self.assertTrue(
         tpsa.compare_da_with_file(self.path + "bunch_substitution_1.txt",
                                   ly[1], self.eps))
     self.assertTrue(
         tpsa.compare_da_with_file(self.path + "bunch_substitution_2.txt",
                                   ly[2], self.eps))
示例#7
0
import pybind11
import tpsa
from math import *

tpsa.da_init(4, 3, 1000)
da = tpsa.base()

x1 = da[0] + 2 * da[1] + 3 * da[2]
x2 = tpsa.sin(x1)
x1 = tpsa.cos(x1)

x3 = 0.5 * da[0] + 4 * da[1] + 2.7 * da[2]
x4 = tpsa.sin(x3)
x3 = tpsa.cos(x3)

y1 = tpsa.complex(x1, x2)
y2 = tpsa.complex(x3, x4)

mmap = tpsa.assign(2)
mmap[0] = x1
mmap[1] = x2

c1 = complex(4.2, 0.3)
c2 = complex(1 / 3.0, sqrt(2))
c3 = complex(sin(0.7), cos(0.4))

nmap = [c1, c2, c3]
omap = tpsa.da_composition(mmap, nmap)
print('Composition of DA vectors with complex numbers:')
for v in omap:
    print(v)
# -*- coding: utf-8 -*-
"""
This example shows how to save a da vector to a file. 
"""

import tpsa
import sys
tpsa.da_init(4, 1, 100)
da = tpsa.base()
x = tpsa.sin(1 + da[0])

f = open("da_output.txt", "w")
ori_stdout = sys.stdout
sys.stdout = f
tpsa.print(x)
sys.stdout = ori_stdout
f.close()
示例#9
0
# -*- coding: utf-8 -*-
"""
This example shows how to create a DA vector x than only contains one variable 
and calculate sin(x). 
Results are printed to screen.  
"""

import pybind11
import tpsa

tpsa.da_init(4, 1, 129)
da = tpsa.base()
x = 1 + da[0]
y = tpsa.sin(x)
x.print()
y.print()