コード例 #1
0
    def run(self):
        logger.info('thread started')
        pltData = {}
        cps = FPS()
        cps.getFPS = lambda: (0 if cps._numFrames == 0 else cps.fps())  # pylint: disable=protected-access
        startTime = epochTime()
        cps.start()
        while True:
            try:
                serialInput = self.handler.read_until(
                    '#'.encode('utf-8')).decode('utf-8')
                logger.info('INPUT: %s', serialInput)
                if not serialInput.endswith('@#'):
                    tiltOutput, panOutput, _ = serialInput.split(' ')
                    pltData['tiltOutput'] = int(tiltOutput)
                    pltData['panOutput'] = int(panOutput)
                    self.outQ.put(pltData)
            except Exception as err:
                logger.error('serial input failed. %s', err)
                continue

            self.readyEvent.set()
            data = self.inQ.get()
            if data is None:
                break
            tiltErr, panErr = [data[key] for key in ('tiltErr', 'panErr')]

            serialOutput = f'{tiltErr * -1} {panErr * -1}$'
            self.handler.write(serialOutput.encode('utf-8'))
            logger.info('OUTPUT: %s', serialOutput)
            cps.update()
            cps.stop()
            pltData['time'] = epochTime() - startTime
            pltData['panErr'] = panErr
            pltData['tiltErr'] = tiltErr

        self.outQ.put(None)

        self.handler.close()
        logger.info('CPS: %s', cps.getFPS())
        logger.info('thread finished')
コード例 #2
0
from time import time as epochTime
import subprocess
import matplotlib.pyplot as plt


def aFunction(x):
    xN = 0.5 * x + 0.5
    return ((xN**3) / (xN + 1)) * math.cos(xN**2)


pf = aFunction
a = 0
b = 1
n = sys.argv[1]
#n=2
start = epochTime()
aSolver = pyGausssolver(pf, a, b, int(n))
aSolver.exec()
print(aSolver.getResult())
print(f"took {(epochTime() - start) * 1000:.4f} ms\n")
start = epochTime()
subprocess.call(['./main', f'{n}'])
print(f"took {(epochTime() - start) * 1000:.4f} ms\n")

pythonTime = []
cTime = []
inputList = range(1, int(n) + 1)
for i in inputList:
    start = epochTime()
    aSolver = pyGausssolver(pf, 0, 1, i)
    aSolver.exec()
コード例 #3
0
import gaussSolver as gs
import math
import subprocess
from time import time as epochTime
import sys


def func(x):
    x = 0.5 * x + 0.5
    return math.pow(x, 3) * math.cos(math.pow(x, 2)) / (x + 1)


n = int(sys.argv[1])

start = epochTime()
aSolver = gs.CGaussSolver(func, 0, 1, n)
aSolver.exec()
print("Python n = {}: {:.20f}".format(n, aSolver.getResult()))
print("python seconds: {:.4f}ms".format((epochTime() - start) * 1000))
start = epochTime()
subprocess.call(['./main', '{}'.format(n)])
print("took {:.4f} ms".format((epochTime() - start) * 1000))

pythonTime = []
cTime = []
inputList = range(1, n + 1)
for i in inputList:
    start = epochTime()
    aSolver = gs.CGaussSolver(func, 0, 1, i)
    aSolver.exec()
    pythonTime.append((epochTime() - start) * 1000)
コード例 #4
0
            answer += 0.5*self.wi(x)*f(0.5*x +0.5)
        return answer

def legendre(i,x):
    if i==0 :
        return 1
    elif i==1:
        return x
    else:
        return (  ((2*i-1)/i)*float(x)*float(legendre(i-1,x)) - ((i-1)/i)*float(legendre(i-2,x)) )

def f(x):
    return (x**3/(x+1))*math.cos(x**2)

def newton_raphson(i,x0):
    x = x0 - (legendre(i,x0)/odlegendre(i,x0))
    if legendre(i,x) > 0.05 :
        return newton_raphson(i,x)
    else:
        return x

def odlegendre(i,x):
    return (i/((x**2)-1))*(x*legendre(i,x) - legendre(i-1,x))

n = input('Enter n')
P=PIntegral( eval(n))
start = epochTime()
print(f'answer is:{P.calculate()}')
end = epochTime()
print(f'took:{end-start}')
コード例 #5
0
    def getResult(self):
        return self.m_Result


def aFunction(x):
    xN = .5 * x + .5
    return (xN**3 / (xN + 1)) * np.cos(xN**2)


n1 = int(input('Degree?'))
a = 0
b = 1
cTime = []
pTime = []
for n in range(n1):
    start = epochTime()
    aSolver = PGaussSolver(aFunction, a, b, n)
    result = aSolver.exec()  # Calculate the integral
    stop = epochTime()
    print(f'Result of Python code (n= {n}):{result}')
    pTime.append(stop - start)

    start = epochTime()
    subprocess.call(["ConsoleApplication5.exe", str(n)])
    stop = epochTime()
    cTime.append(stop - start)

table = [(n, cTime[n], pTime[n]) for n in range(n1)]
print(tabulate(table, headers=['num', 'C++ timer', 'Python timer']))
#subprocess.call(["ConsoleApplication5.exe",str(5)])
コード例 #6
0
ファイル: Q6-b.py プロジェクト: sepehr-javanmardi/AP-hw5
from time import time as epochTime
import subprocess
import PGS
from prettytable import PrettyTable
import matplotlib.pyplot as plt
import numpy as np

columns = ('Degree of legender polynominal', 'Python running time(ms)',
           'C++ running time(ms)')
t = PrettyTable(columns)
PT = []
CT = []
for i in range(20):
    d = i + 1

    start = epochTime()
    a = PGS.PGussSolver(d)
    a.newton(0.001, 1000)
    a.Integrate(lambda x: x**3 / (x + 1) * cmath.cos(x**2), 0, 1)
    PTime = (epochTime() - start) * 1000
    PT.append(PTime)

    start = epochTime()
    subprocess.call(["ConsoleApplication2.exe",
                     str(d)])
    CTime = (epochTime() - start) * 1000
    CT.append(CTime)
    t.add_row([i + 1, PTime, CTime])

print(t)
rows = [
コード例 #7
0
ファイル: Q6-a.py プロジェクト: sepehr-javanmardi/AP-hw5
#Question6
import cmath
from time import time as epochTime
import subprocess
#PGS is a module
import PGS

print("please inter degree of legender:")
d = int(input())

start = epochTime()
#Make an object
a = PGS.PGussSolver(d)
#Set the presition of newton method
a.newton(0.001, 1000)
print("Result of Puthon code (n =", d, ") :",
      a.Integrate(lambda x: x**3 / (x + 1) * cmath.cos(x**2), 0, 1))
print("Python Running time is :", (epochTime() - start) * 1000, "ms")

start = epochTime()
subprocess.call(["ConsoleApplication2.exe", str(d)])
print("C++ Running time is :", (epochTime() - start) * 1000, "ms")
コード例 #8
0
ファイル: q6.py プロジェクト: vahid-aki/AP_HW5
                self.legendreZeroes(self.m_N, i)) * self.weight(
                    self.m_N, self.legendreZeroes(self.m_N, i))
        self.m_Result = ((self.m_B - self.m_A) / 2.0) * integral

    def getResult(self):
        return self.m_Result

    def aFunction(self, x):
        xN = 0.5 * x + 0.5
        return (pow(xN, 3) / (xN + 1)) * math.cos(pow(xN, 2))


if __name__ == '__main__':
    a, b = 0, 1
    n = int(sys.argv[1])
    aSolver = CGaussSolver(a, b, n)
    aSolver.exec()
    print(f'Result of python code (n = {n} ): {aSolver.getResult()}')
    subprocess.call([os.path.join(os.getcwd(), 'HW5.exe'), str(n)])
    print('\n')

    for i in range(10):
        p_t1 = epochTime()
        aSolver = CGaussSolver(a, b, i)
        aSolver.exec()
        aSolver.getResult()
        p_t2 = epochTime()

        subprocess.call([os.path.join(os.getcwd(), 'HW5.exe'), str(i)])
        print(f' n = {i} & Python took: {(p_t2 - p_t1)*1000} ms\n')