コード例 #1
0
ファイル: task1.py プロジェクト: thestics/calculus
def sub_task2(acoef: list, bcoef: list):  # direct product
    t1 = time.time()
    p1 = poly(acoef)
    p2 = poly(bcoef)
    prod = p1 * p2
    t = time.time() - t1
    print(f'sub_task2 Time elapsed: {t}')
    return list(prod.coef)
コード例 #2
0
def exaggerated_poly(gauss20, exaggeration=50):
    #Outputs:
    #  x_poly - list of x points for exaggerated poly
    #  y_poly_final - list of y points for exaggerated poly
    #  idxs_to_adjust - list of indexs of x_poly where jump occurs

    # x and y inputs for RANSAC
    x = np.linspace(0, len(gauss20) - 1, num=len(gauss20))
    y = np.asarray(gauss20)

    # poly = polynomial fit by RANSAC
    _, poly = RANSAC_poly(x, y, 1000, 5)
    # y points for polynomial
    y_poly = poly(x)

    # Take left most point, highest point + exaggeration (but minus cause images), right most point, then fit new polynomial to those

    xs_fit = [0, np.argmin(y_poly), len(gauss20) - 1]
    ys_fit = [
        y_poly[0],
        np.min(y_poly) - exaggeration, y_poly[len(gauss20) - 1]
    ]

    new_poly = poly_reg(xs_fit, ys_fit)

    # final polynomial
    y_poly_final = new_poly(x)
    return x, y_poly_final, y_poly
コード例 #3
0
ファイル: rLocus.py プロジェクト: Nestyko/Root_Locus_Plot
def input_poly(lang):
    lan = language[lang]

    pgrade = input_int(lan['input_poly'], lang)

    p = [0]*(pgrade+1)
    for i in range(pgrade+1):
        p[i] = input_float("x^" + str(i) + ": ", lang)
    return poly(p)
コード例 #4
0
def enCrypt(pt, pk, q, t, modP):
    l = pk[0].coef.size #key length
    m = poly(pt % t)
    d = int(q / t) #delta to regulate the 
    e1 = rnd_normal_coef(l)
    e2 = rnd_normal_coef(l)
    u = rnd_binary_coef(l)
    
    ct0 = polyaddMod(polyaddMod(polymulMod(pk[0], u, q, mp), e1, q, modP), d * m, q, modP)
    ct1 = polyaddMod(polymulMod(pk[1], u, q, mp), e2, q, modP)
    
    return (ct0, ct1) #the cypher text consists of two polynomials
コード例 #5
0
def rnd_normal_coef(length):
    return poly(np.random.normal(0, 2, length))
コード例 #6
0
def rnd_uniform_coef(length, modS):
    return poly(np.random.randint(0, modS, length))
コード例 #7
0
def rnd_binary_coef(length):
    return poly(np.random.randint(0, 2, length))
コード例 #8
0
def polymulMod(x_1, x_2, modS, modP):
    return poly((poly((x_1 * x_2).coef % modS) % modP).coef % modS)
コード例 #9
0
def polyaddMod(x_1, x_2, modS, modP):
    return poly(((x_1 + x_2).coef % modS)) % modP
コード例 #10
0
    return (ct0, ct1) #the cypher text consists of two polynomials
    
"""
Decryption function, retrieving the plain text by applying the secret key as the base of the vector space to filtre out the noise terms
"""
def deCrypt(ct, sk, q, t, modP):
    dpt = polyaddMod(polymulMod(ct[1], sk, q, mp), ct[0], q, modP)
    
    dt = (dpt.coef * t/q) % t
    return round(dt[0],0)
    
"""
Let's try it out
"""
ms = 11  #the modulus defining the integer residual class Z/{ms}
mp = poly([1,0,0,0,1]) #the modulus defining the polynomial residual class Z(ms)/{mp}

k = keyGenerator(20,ms,mp) #generating the key

pk = k[0] #public key
sk = k[1] #secret key

q = 7919 #q and t defining the noise via the delta d
t = 17

pt1 = 2 #plain text 1
pt2 = 4 #plain text 2

ct1 = enCrypt(pt1, pk, q, t, mp) #cypher text of plain text 1 using only the public key
ct2 = enCrypt(pt2, pk, q, t, mp) #cypher text of plain text 2
コード例 #11
0
"""

#def test_profile(slow=True):
# Import required librariesimpo
import numpy as np
from numpy.polynomial.polynomial import polyfit
from numpy.polynomial import Polynomial as poly


def fun(x):
    return x**2 + np.sin(x) - 1


x = np.random.rand(100, 100)
y = np.zeros_like(x)
IT1 = range(x.shape[0])
IT2 = range(x.shape[1])
slow = True
if slow == True:
    for i in IT1:
        for j in IT2:
            coef = polyfit([0, 1, 4, 5], [-1, 2, 1, 4], 3)
            p = poly(coef)
            y[i, j] = fun(x[i, j]) / 2 + p(x[i, j])
else:
    coef = polyfit([0, 1, 4, 5], [-1, 2, 1, 4], 3)
    p = poly(coef)
    for i in IT1:
        for j in IT2:
            y[i, j] = fun(x[i, j]) / 2 + p(x[i, j])
コード例 #12
0
# Construct a fit using the given points
# The region we are dealing with is small, each dimension is 6.65 millionth of the total radius @ 45deg latitude
# So assume that a linear fit is accurate, but our points have some error
# Thus, we do a linear regression
given_lats = [];
given_x = [];
given_longs = [];
given_y = [];
for point in im_json['points']:
	given_lats.append(point['lat']);
	given_x.append(point['x']);
	given_longs.append(point['long']);
	given_y.append(point['y']);
slope_x, intercept_x, r_value_x, p_value_x, std_err_x = stats.linregress(given_longs,given_x);
slope_y, intercept_y, r_value_y, p_value_y, std_err_y = stats.linregress(given_lats,given_y);
long2x = poly([intercept_x, slope_x])
lat2y = poly([intercept_y,slope_y]);

# Now take the GPs coordinates of the damaged panels and
for coords in id_json['damaged']:
	lat = coords['lat'];
	long = coords['long'];
	msg = coords['message'];
	
	# Convert them to their pixel values
	px = long2x(long);
	py = lat2y(lat);
	
	# at x,y the image is drawn to the right and then bottom
	# So we go -width/2 and -height
	pxd = int(px - pin_image_width/2);
コード例 #13
0
#Homework3.10   用ploy函数将下列多项式表示为幂级数形式y = 5 * (x - 3) * (x - 4) * (x + 1) * (x + 3)

from numpy.polynomial import Polynomial as poly

p0 = poly(( 5))
p1 = poly((-3, 1))
p2 = poly((-4, 1))
p3 = poly(( 1, 1))
p4 = poly(( 3, 1))

p = p0 * p1 * p2 * p3 * p4

result = list(p.coef)

print('result = {0[0]} + {0[1]} * x + {0[2]} * (x ** 2) + {0[3]} * (x ** 3) + {0[4]} * (x ** 4)'.format(result))
コード例 #14
0
def apply_poly(x, poly):
    # returns corresponding y points to the x points given, given the polynomial function
    x_new = np.linspace(x[0], x[-1], num=len(x))
    exp_x_new = np.expand_dims(x_new, axis=1)
    exp_y_new = np.expand_dims(poly(x_new), axis=1)
    return np.concatenate([exp_x_new, exp_y_new], axis=1)