import faulthandler
faulthandler.enable()

import numpy as np
from numpy.testing import assert_allclose
from reneu.lib.skeleton import pca_first_component
from math import isclose
from sklearn.decomposition import PCA

def python_pca(X):
    pca = PCA(n_components=1)
    pca.fit(X)
    component = pca.components_[0]
    return component

def test_pca():
    X = np.asarray(range(0, 12), dtype=np.float32).reshape(4,3)
    # this is the result of above code
    component = np.array([-0.5773503 , -0.5773502 , -0.57735026], dtype=np.float32)
    # somehow the sklearn component are all negative
    # inverting them should not change the result of orientation
    # since there is no direction
    component = -component

    component2 = pca_first_component( X )
    assert_allclose(component, component2, rtol=1e-4)
    # the vector should already be normalized
    assert isclose(np.linalg.norm( component2 ), 1, rel_tol=1e-4)

    X = np.array(  [[261680., 162640., 790605.],
                    [261680.,  162640.,  790740.],
                    [261680.,  162640.,  790695.],
                    [261680.,  162640.,  791010.],
                    [261680.,  162640.,  790965.],
                    [261680.,  162640.,  790920.],
                    [261680.,  162640.,  790875.],
                    [261680.,  162640.,  791055.],
                    [261680.,  162640.,  790830.],
                    [261680.,  162640.,  790785.],
                    [261680.,  162640.,  790650.],
                    [261680.,  162640.,  790470.],
                    [261680.,  162480.,  790380.],
                    [261680.,  161840.,  789750.],
                    [261680.,  162560.,  790425.],
                    [261680.,  162640.,  790515.],
                    [261680.,  161920.,  789840.],
                    [261760.,  162000.,  789885.],
                    [261680.,  161840.,  789795.],
                    [261760.,  162000.,  789930.]])
    print('shape of input array: ', X.shape)
    # component = python_pca(X)
    component1 = np.asarray([ 0.02229965, -0.57740044, -0.81615652 ])
    print('python pca output: ', component1)
    component2 = pca_first_component(X)
    component3 = pca_first_component(X)
    component4 = pca_first_component(X)
    np.testing.assert_equal(component2, component3)
    np.testing.assert_equal(component2, component4)
    # print('out pca output: ', pca_first_component(X))
    np.testing.assert_almost_equal( -component1,  component2, decimal=5)


import numpy as np
import pprint

def random_A_and_B_matrix() -> np.array:
    """Genereting two matrix

    Returns:
        two np.array
    """
    a = np.random.randint(0, 5, size=(3, 3))
    b = np.random.randint(0, 5, size=(3, 1))
    return a, b

def find_matrix_X(matrix_A_inv: np.array, matrix_B: np.array) -> np.array:
    """Function for finding X matrix

    Args:
        matrix_A (np.array): matrix with shape (3,3)
        matrix_B (np.array): matrix with shape (3,1)

    Returns:
        np.array: result of multiplying two matrix
    """
    matrix_result = np.matmul(matrix_A_inv, matrix_B)
    return matrix_result

def main():
    # first matrix, shape (3,3)
    matrix_A = np.array([[1, 2, 3], [0, 1, 2], [2, 0, 0]])
    # print(np.linalg.inv(matrix_A))

    # second matrix, shape (3,1)
    matrix_B = np.array([[1], [1], [0]])

    # find X matrix
    matrix_X = find_matrix_X(np.linalg.inv(matrix_A), matrix_B)
    print(f"Result X matrix - {matrix_X}")

    # random testing
    try:
        test_A_matrix, test_B_matrix = random_A_and_B_matrix()
        print(find_matrix_X(np.linalg.inv(test_A_matrix), test_B_matrix))
    except np.linalg.LinAlgError as e:
        print(
            f"Some errors occured.\nImpossible to find the inverse matrix.\nThe given matrix is - {e}"
        )

if __name__ == "__main__":
    main()


import numpy as np

n= int(input("Number of points(order) (Enter between 1 to 6): "))
xi= int(input("Initial value of integration: "))
xf= int(input("Upper limit of integration: " ))
def guass_Quadrature(xi,xf,n):

    def f1(z):
        return 5 * z * np.exp(-2 * z)

    # def f2(z):
    #    return z ** 6 - 7 * z ^ 3

    def f3(z):
        return (xf-xi)/2*(f1((xf+xi)/2+z*(xf-xi)/2))

    y=0
    if n==1:
        w=[2]
        x=[0]

    elif n==2:
        w=[1.0000000000000000,1.0000000000000000]
        x=[-0.5773502691896257,0.5773502691896257]
        # for i in range(0,2):
        #    y+=w[i]*f1(x[i])
        # print(y)
    elif n==3:
        w=[0.8888888888888888,0.5555555555555556,0.5555555555555556]
        x=[0.0000000000000000,-0.7745966692414834,0.7745966692414834]
        # for i in range(0,3):
        #    y+=w[i]*f1(x[i])
        # print(y)

    elif n==4:
        w=[0.6521451548625461,0.6521451548625461,0.3478548451374538,0.3478548451374538]
        x=[-0.3399810435848563,0.3399810435848563,-0.8611363115940526,0.8611363115940526]
        # for i in range(0,4):
        #    y+=w[i]*f1(x[i])
        # print(y)

    elif n==5:
        w=[0.5688888888888889,0.4786286704993665,0.4786286704993665,0.2369268850561891,0.2369268850561891]
        x=[0.0000000000000000,-0.5384693101056831,0.5384693101056831,-0.9061798459386640,0.9061798459386640]
        # for i in range(0,5):
        #    y+=w[i]*f1(x[i])
        # print(y)

    elif n==6:
        w=[0.3607615730481386,0.3607615730481386,0.4679139345726910,0.4679139345726910,0.1713244923791704,0.1713244923791704]
        x=[0.6612093864662645,0.6612093864662645,0.2386191860831969,-0.2386191860831969,0.9324695142031521,-0.9324695142031521]
        # for i in range(0,6):
        #    y+=w[i]*f1(x[i])
        # print(y)

    else:
        print("invalid number of input")

    y=0
    if xi==-1 and xf ==1:
        for i in range(0,n):
            y+=(w[i]*f1(x[i]))
        print("The value of integration is: ",y)
    else:
        for i in range(0,n):
            y+=w[i]*f3(x[i])
        print("The value of integration is: ",y)

print(guass_Quadrature(xi,xf,n))


"""
page 103
"""
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

T = [1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968]
S = [29.72, 30.61, 31.51, 32.13, 32.34, 32.85, 33.56, 34.20, 34.83]

xdata = np.array(T)
ydata = np.log(np.array(S))

def func(x, a, b):
    return a + b * x

popt, pcov = curve_fit(func, xdata, ydata)
print(popt)
print(pcov)

plt.plot(xdata, ydata, 'ko', label="Original Noised Data")
plt.plot(xdata, func(xdata, *popt), 'r', label="Fitted Curve")
plt.show()


from __future__ import division
import math
import numpy as np

def cal_mse(x, y):
    return ((x - y)**2).mean()

def cal_cross_entropy(x, y):
    """ Computes cross entropy between two distributions.
    Input: x: iterabale of N non-negative values
           y: iterabale of N non-negative values
    Returns: scalar
    """

    if np.any(x < 0) or np.any(y < 0):
        raise ValueError('Negative values exist.')

    # Force to proper probability mass function.
    x = np.array(x, dtype=np.float)
    y = np.array(y, dtype=np.float)
    x /= np.sum(x)
    y /= np.sum(y)

    # Ignore zero 'y' elements.
    mask = y > 0
    x = x[mask]
    y = y[mask]
    ce = -np.sum(x * np.log(y))
    return ce

def cal_sigmoid(x):
    return 1. / (1. + np.exp(-x))

def cal_softmax(x, t=-100.):
    x = x - np.max(x)
    if np.min(x) < t:
        x = x / np.min(x) * t
    exp_x = np.exp(x)
    out = exp_x / np.sum(exp_x, axis=-1, keepdims=True)
    return out

def cal_sqnr(signal_raw, signal_dequant, remove_zero=True):
    # SQNR is non-commutative
    # Unlike other distance function
    # Cannot change the order of signal_raw and signal_dequant
    raw = signal_raw.flatten()
    dequant = signal_dequant.flatten()

    if remove_zero is True:
        idx = dequant != 0
        raw = raw[idx]
        dequant = dequant[idx]

    noise = raw - dequant

    avg_raw = np.sum(raw) / raw.size
    avg_noise = np.sum(noise) / noise.size

    raw_zero_mean = raw - avg_raw
    noise_zero_mean = noise - avg_noise

    var_raw_zero_mean = np.sum(np.square(raw_zero_mean))
    var_noise_zero_mean = np.sum(np.square(noise_zero_mean))

    if var_noise_zero_mean == 0.0:
        return math.inf

    sqnr = 10 * np.log10(var_raw_zero_mean / var_noise_zero_mean)

    return sqnr

if __name__ == '__main__':
    raw = np.array([5.2, -4.1, 3.14, -2.1, 3.8, -1.2])
    dequant = np.array([5, -4, 3, -2, 4, -1])
    print(cal_sqnr(raw, dequant))


# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# In[2]:

df = pd.read_csv('Housedata.csv', index_col=0)
df = df.replace({'yes': 1, 'no': 0})
df.head()

# In[3]:

Y = df.iloc[:, 0].values
X = df.iloc[:, 1:].values

# In[21]:

w = np.linalg.inv(np.matmul(X.T , X))  #(X.T * X)^-1

# In[22]:

y = np.matmul(X.T , Y)  #(X.T * Y)

# In[23]:

w = np.matmul(w , y)

# In[24]:

w

# In[19]:

index = 25
np.dot(w , X[index]), Y[index]

# In[36]:

Y_pred = np.array([np.dot(w, x) for x in X])

# In[37]:

Y_pred


from math import atan2
import numpy as np
import matplotlib.pyplot as plt

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

def graham_scan(coords):
    """Sort list so that first point is the lowest point in the numpy array"""
    coords = coords[np.argsort(coords[:, -1])]
    if len(coords) <= 4:
        raise RuntimeError("Convex hull not possible with less than three points")
    try:
        if coords[0][1] == coords[1][1]:
            """Sort by the y value then the x value if two y values are the same to find leftmost point"""
            coords = coords[np.lexsort((coords[:, 0], coords[:, 1]))]
    except IndexError:
        raise RuntimeError("Convex Hull not possible with less than three points")
    """Get Lowest Point then remove from the numpy array"""
    lowest_point = coords[0]
    p0 = Point(lowest_point[0], lowest_point[1])
    coords = coords[1:]     # Remove first value which is lowest point
    coords = np.array([[x, y, polar_angle(p0, Point(x, y))] for (x, y) in zip(coords.flat[0::2], coords.flat[1::2])])
    """Sort By Polar Angle W.R.T lowest point p0"""
    coords = coords[np.argsort(coords[:, -1])]
    coords = coords[:, [0, 1]]  # Remove polar angle once sorted
    coords = [Point(x, y) for (x, y) in zip(coords.flat[0::2], coords.flat[1::2])]
    sorted_points = coords
    hull = [p0, sorted_points[0]]
    del sorted_points[0]
    for point in sorted_points:
        while orientation(point, hull[-1], hull[-2]) <= 0:
            hull.pop()
        hull.append(point)

    return hull

def orientation(a, b, c):
    return (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y)

def polar_angle(p0, p1):
    return atan2(p1.y - p0.y, p1.x - p0.x)

def benchmark(test_sizes):
    for size in test_sizes:
        coordinates = np.random.rand(size, 2)
        coords = [Point(x, y) for (x, y) in zip(coordinates.flat[0::2], coordinates.flat[1::2])]
        x_vals = [i.x for i in coords]
        y_vals = [i.y for i in coords]
        hull = graham_scan(coordinates)
        hull_x_vals = [i.x for i in hull]
        hull_y_vals = [i.y for i in hull]
        plt.scatter(x_vals, y_vals, color='green')
        plt.scatter(hull_x_vals, hull_y_vals, color='red')
        plt.show()

if __name__ == "__main__":
    sizes = [10, 50, 75, 100, 1000]
    benchmark(sizes)


import numpy as np
import matplotlib.pyplot as plt
import numpy.linalg as la

datosFull = np.loadtxt('numeros_20.txt')
x = datosFull[:,0]
y = datosFull[:,1]

# Primera Parte ---------------------------------------------------------------------------------------------------------

def fitCoef(x,y,M):
    S = np.power(np.transpose(np.repeat(x[None],M+1,axis=0)),np.linspace(0,M,M+1))
    Sinv = la.pinv(S) #Si se puede, usar pseudoinversa implementada en linalg
    return np.dot(Sinv,y)
def polyGen(coef,x):
    M = len(coef)-1
    powers = np.power(np.transpose(np.repeat(x[None],M+1,axis=0)),np.linspace(0,M,M+1))
    return np.sum(np.multiply(powers,coef),axis=1)
def chiSquare(y,t):
    return np.sum(np.power(np.subtract(y,t),2))

fig,axii = plt.subplots(2,2)

axes = list(axii[0])+list(axii[1])
Ms = [0,1,3,9]
E_rms = [[],[]]
for i in range(0,10):

    x_test = np.linspace(0,1)
    coefs = fitCoef(x[:10],y[:10],i)
    y_fit = polyGen(coefs,x_test) #Si era el otro fit, cambiar esto.

    E_rms[0].append(np.sqrt(chiSquare(polyGen(coefs,x[:10]),y[:10]))/10)
    E_rms[1].append(np.sqrt(chiSquare(polyGen(coefs,x[10:]),y[10:]))/10)

    if i in Ms:
        a = axes[Ms.index(i)]
        a.scatter(x[10:],y[10:],facecolors='none',edgecolors='b')
        a.scatter(x[:10],y[:10],c='grey')
        a.plot(x_test,y_fit,c='r')
        a.set_title("M = "+str(i))
        a.set_ylim(0,2)
        if i == 9:
            a.set_ylim(-2000,2000)

fig.tight_layout()
fig.savefig('PrimeraParte_Figura1.png')

Ms = range(0,10)

fig = plt.figure()
plt.plot(Ms,E_rms[0],c='r',label='Datos entrenamiento')
plt.scatter(Ms,E_rms[0],c='r')
plt.plot(Ms,E_rms[1],c='b',label='Datos prueba')
plt.scatter(Ms,E_rms[1],c='b')
plt.yscale('log')
plt.legend()
plt.savefig('PrimeraParte_Figura2.png')

# Segunda Parte ---------------------------------------------------------------------------------------------------------------

class PolyFit():
    def __init__(self,degree=0):
        self.M = degree+1
        self.betas = np.array([])
    def fit(self,x,y):
        S = np.power(np.transpose(np.repeat(x[None],self.M,axis=0)),np.linspace(0,self.M-1,self.M))
        Sinv = la.pinv(S)
        self.betas = np.dot(Sinv,y)
    def predict(self,x):
        powers = np.power(np.transpose(np.repeat(x[None],self.M,axis=0)),np.linspace(0,self.M-1,self.M))
        return np.sum(np.multiply(powers,self.betas),axis=1)
    def score(self,x,y):
        chi = np.sum(np.power(np.subtract(self.predict(x),y),2))
        return np.sqrt(chi/len(y))

fig,axii = plt.subplots(2,2)

axes = list(axii[0])+list(axii[1])
Ms = [0,1,3,9]
E_rms = [[],[]]
for i in range(0,10):
    hola = PolyFit(i)
    x_test = np.linspace(0,1)
    coefs = hola.fit(x[:10],y[:10])
    y_fit = hola.predict(x_test)

    E_rms[0].append(hola.score(x[:10],y[:10]))
    E_rms[1].append(hola.score(x[10:],y[10:]))

    if i in Ms:
        a = axes[Ms.index(i)]
        a.scatter(x[10:],y[10:],facecolors='none',edgecolors='b')
        a.scatter(x[:10],y[:10],c='grey')
        a.plot(x_test,y_fit,c='r')
        a.set_title("M = "+str(i))
        a.set_ylim(0,2)
        if i == 9:
            a.set_ylim(-2000,2000)

fig.tight_layout()
fig.savefig('SegundaParte_Figura1.png')

Ms = range(0,10)

fig = plt.figure()
plt.plot(Ms,E_rms[0],c='r',label='Datos entrenamiento')
plt.scatter(Ms,E_rms[0],c='r')
plt.plot(Ms,E_rms[1],c='b',label='Datos prueba')
plt.scatter(Ms,E_rms[1],c='b')
plt.yscale('log')
plt.legend()
plt.savefig('SegundaParte_Figura2.png')


"""
  Name     : c15_08.py
  Book     : Python for Finance (2nd ed.)
  Publisher: Packt Publishing Ltd.
  Author   : Yuxing Yan
  Date     : 6/6/2017
  email    : yany@canisius.edu
             paulyxy@hotmail.com
"""
import numpy as np
import scipy as sp
import statsmodels.api as sm
#
def breusch_pagan_test(y,x):
    results=sm.OLS(y,x).fit()
    resid=results.resid
    n=len(resid)
    sigma2 = sum(resid**2)/n
    f = resid**2/sigma2 - 1
    results2=sm.OLS(f,x).fit()
    fv=results2.fittedvalues
    bp=0.5 * sum(fv**2)
    df=results2.df_model
    p_value=1-sp.stats.chi.cdf(bp,df)
    return round(bp,6), df, round(p_value,7)
#
sp.random.seed(12345)
n=100
x=[]
error1=sp.random.normal(0,1,n)
error2=sp.random.normal(0,2,n)
for i in range(n):
    if i%2==1:
        x.append(1)
    else:
        x.append(-1)
#
y1=x+np.array(x)+error1
y2=sp.zeros(n)
#
for i in range(n):
    if i%2==1:
        y2[i]=x[i]+error1[i]
    else:
        y2[i]=x[i]+error2[i]

print ('y1 vs. x (we expect to accept the null hypothesis)')
bp=breusch_pagan_test(y1,x)
#
print('BP value,	df,	p-value')
print 'bp =', bp
bp=breusch_pagan_test(y2,x)
print ('y2 vs. x	(we expect to rject the null hypothesis)')
print('BP value,	df,	p-value')
print('bp =', bp)

import numpy,import scipy,import statsmodels


#!/bin/python
# -*- UTF-8 -*-
# 1. import
import numpy as np
from matplotlib.pyplot import plot
from matplotlib.pyplot import show
# 2. Prepare data
bhp = np.loadtxt("BHP.csv", delimiter=',', usecols=(6, ), unpack=True)
vale = np.loadtxt("vale.csv", delimiter=',', usecols=(6, ), unpack=True)
# 2. Covariance & coefficient
bhp_returns = np.diff(bhp) / bhp[: -1] #(x1-x0) / x0.
vale_returns = np.diff(vale) / vale[: -1]
covariance = np.cov(bhp_returns, vale_returns)
print("cov matrix", covariance)
print("cov matrix diagonal: ", covariance.diagonal())
print("cov matrix trace: ", covariance.trace())
print("corr matrix", covariance / (bhp_returns.std(ddof=1) * vale_returns.std(ddof=1)))
print("corr matrix", np.corrcoef(bhp_returns, vale_returns))
# Sync
avg_diff = np.mean(bhp - vale)
dev = np.std(bhp - vale)
print("Sync: ", np.abs(bhp[-1] - vale[1] - avg_diff) > 2 * dev)
# plot
t = np.arange(len(bhp_returns))
# show
#plot(t, bhp_returns, lw=1)
#plot(t, vale_returns, lw=2)
#show()
# 4.3 4.4 Polyfit
t = np.arange(len(bhp))
poly_diff = np.polyfit(t, bhp - vale, 3)
print("Polynomial fit", poly_diff)
print("Next value", np.polyval(poly_diff, t[-1] + 1))
# roots
print("Roots", np.roots(poly_diff))
# der
der = np.polyder(poly_diff)
print("Derivative", der)
# der roots
print("Extremas", np.roots(der))
# check
vals = np.polyval(poly_diff, t)
print("max point", np.argmax(vals))
print("min point", np.argmin(vals))

# show
#plot(t, bhp - vale)
#plot(t, vals)
#how()

#


import numpy as np
import matplotlib.pyplot as plt

#Llevando nuestro algoritmo a python

def estimate_b0_b1(x, y):
    n = np.size(x)
    #obtenemos los promedios de X y de Y
    m_x, m_y = np.mean(x), np.mean(y)

    #Calcular sumatoria de XY y mi sumatoria de XX
    Sumatoria_xy = np.sum((x-m_x)*(y-m_y))
    Sumatoria_xx = np.sum(x*(x-m_x))

    #coeficientes de regresion
    b_1 = Sumatoria_xy / Sumatoria_xx
    b_0 = m_y - b_1*m_x

    return(b_0, b_1)

#Funcion de graficado
def plot_regression(x, y, b):
    plt.scatter(x, y, color = "g", marker = "o", s=30)

    y_pred = b[0] + b[1]*x
    plt.plot(x, y_pred, color = "b")

    #etiquetado
    plt.xlabel('x-Independiente')
    plt.ylabel('y-Dependiente')

    plt.show()

    # Codigo MAIN
def main():
        # DATASET
        x = np.array([1, 2, 3, 4, 5])
        y = np.array([2, 3, 5, 6, 5])

        # Obtenemos b1 y b2
        b = estimate_b0_b1(x, y)
        print("Los valores b0 = {}, b1 ={}".format(b[0], b[1]))

        # Graficamos nuestra linea de regresion
        plot_regression(x, y, b)

if __name__ == "__main__":
    main()


#
# Created by Homayoun Heidarzadeh
#
import numpy as np

# Efficient row-wise distances

class EuclideanRowWise:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        x = args[0]
        matrix = args[1]
        return np.sum(np.abs(x - matrix) ** 2, axis=-1) ** (1. / 2)

    def __repr__(self):
        return "euclidean"

class ManhattanRowWise:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        x = args[0]
        matrix = args[1]
        return np.sum(np.abs(x - matrix), axis=-1)

    def __repr__(self):
        return "manhattan"

class CosineRowWise:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        x = args[0]
        matrix = args[1]
        return 1 - np.sum(matrix * x, -1) / ((np.sum(matrix ** 2, axis=-1) ** (1. / 2)) * np.linalg.norm(x))

    def __repr__(self):
        return "Cosine"

class MinkowskiRowWise:
    def __init__(self, p):
        self.p = p

    def __call__(self, *args, **kwargs):
        x = args[0]
        matrix = args[1]
        return np.sum(np.abs(x - matrix) ** self.p, axis=-1) ** (1. / self.p)

    def __repr__(self):
        return "Minkowski(p=%1.1f)" % self.p


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# --- Rot Motion
freq1= 0.8
freq2= 0.4
tMax = 10
dt   = 0.1

omega1 = 2*np.pi*freq1
T1  = 1/freq1
omega2 = 2*np.pi*freq2
T2  = 1/freq2

time  = np.arange(0,tMax+dt/2,dt)

pos = np.zeros((len(time), 6)) # positions: x,y,z, theta_x, theta_y, theta_z
vel = np.zeros((len(time), 6)) # velocities: xdot, ydot, zdot, and omega
acc = np.zeros((len(time), 6)) # accelerations: xddot, omegadot

# -- First period we do a vertical motion
I1 = time <= T1
pos[I1,2] =           2*np.sin(omega1*time[I1])
vel[I1,2] = omega1   *2*np.cos(omega1*time[I1])
acc[I1,2] =-omega1**2*2*np.sin(omega1*time[I1])

# -- Second period we do nothing
I2 = time > T1

# -- Third period we do x rotations
I3 = time > 2*T1

pos[I3,3] =            0.10 *np.sin(omega2*time[I3])
vel[I3,3] = omega2   * 0.10 *np.cos(omega2*time[I3])
acc[I3,3] =-omega2**2* 0.10 *np.sin(omega2*time[I3])

cols = ['time_[s]', 'x_[m]', 'y_[m]', 'z_[m]' , 'theta_x_[rad]', 'theta_y_[rad]', 'theta_z_[rad]']
cols +=['xdot_[m/s]', 'ydot_[m/s]', 'zdot_[m/s]', 'omega_x_g_[rad/s]', 'omega_y_g_[rad/s]', 'omega_z_g_[rad/s]']
cols +=['xddot_[m^2/s]', 'yddot_[m^2/s]' , 'zddot_[m^2/s]',  'alpha_x_g_[rad/s]', 'alpha_y_g_[rad/s]', 'alpha_z_g_[rad/s]']

data=np.column_stack((time, pos, vel, acc))

df = pd.DataFrame( data=data, columns=cols)
df.to_csv('KiteMotionSimple.csv', index=False, sep=',', float_format='%10.6f')

print(df.shape)

# Time_[s] , x_[m], y_[m], z_[m] , theta_x_[rad], theta_y_[rad], theta_z_[rad-], xdot_[m/s], ydot_[m/s], zdot_[m/s], omega_x_g_[rad/s], omega_y_g_[rad/s], omega_z_g_[rad/s],  xddot_[m^2/s], yddot_[m^2/s] , zddot_[m^2/s],  alpha_x_g_[rad/s], alpha_y_g_[rad/s], alpha_z_g_[rad/s]

if __name__ == '__main__':
    pass


import numpy as np
A = np.arange(2, 14).reshape((3, 4))
B = np.arange(14, 2, -1).reshape((3, 4))  # -1代表步长
print(A)
print(np.argmin(A))  # 计算最小值索引
print(np.argmax(A))  # 计算最大值索引
print(np.mean(A))  # 计算平均值
print(A.mean())
print(np.average(A))  # 计算平均值
print(np.median(A))  # 计算中位数
print(np.cumsum(A))  # 数组个数仍然一样，但是第二个数是前两数之和，第三数是前三数之和以此类推
#  [ 2  5  9 14 20 27 35 44 54 65 77 90]
print(np.diff(A))  # 后一个数减去前一个数的差
"""[[1 1 1]
 [1 1 1]
 [1 1 1]]"""
print(np.nonzero(A))
"""(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64),
array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64)) 前面一个数组代表横坐标后一个数组代表纵坐标"""
print(np.sort(A))
"""[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]"""
print(A.T.dot(A))  # 矩阵转置
print(np.clip(A, 5, 9))  # 将小于5的数划归到5，大于9的数划归到9，范围之中的数保持不变
print(np.mean(A, axis=0))  # axis表示维数，0表示列，1表示行进行计算mean代表平均数值


import numpy as np

def wmean(x, w=None):
    """ weighted mean """
    if w is None:
        return np.mean(x)
    else:
        return np.sum(x * w) / np.sum(w)

def wstd(x, w=None):
    """
    Weighted standard deviation

    Parameters
    ----------
    x : array
        data
    w : array
        weights

    Returns
    -------
    wstd

    Ref
    ---
    https://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weightsd.pdf

    """
    if w is None:
        w = np.ones_like(x)
    if len(x) < 1:
        return 0.
    else:
        _n_nonzero = np.sum(w>0)
        if _n_nonzero > 1:
            _wmean = wmean(x, w)
            return np.sqrt(np.sum(w*(x-_wmean)**2) / np.sum(w) /(_n_nonzero-1)*_n_nonzero)
        else:
            return 0.


'''
Communicability centrality, also called subgraph centrality, of a node n is the sum of closed walks of all lengths starting and ending at node n.

'''
import networkx as nx

def calculate(network):
    try:
        n = nx.communicability_centrality(network)
    except:
        return 0

    if len(n.values()) == 0:
        return 0
    else:
        return round(sum(n.values())/len(n.values()), 7)


from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target

#naive approach
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=1)
#train the model
model.fit(X, y)
y_model = model.predict(X)
#check accuracy
from sklearn.metrics import accuracy_score
print(accuracy_score(y, y_model))

#this trains and evaluates the model on the same data, its a false 100% accuracy

##The right way:  Holdout sets
from sklearn.model_selection import train_test_split
# split the data with 50% in each set
X1, X2, y1, y2 = train_test_split(X, y, random_state=0,
                                  train_size=0.5)
#fit the model on one set
model.fit(X1, y1)

#evaluate accuracy on the other
y2_model = model.predict(X2)
print(accuracy_score(y2, y2_model))

##Model validation via cross-validation
y2_model = model.fit(X1, y1).predict(X2)
y1_model = model.fit(X2, y2).predict(X1)
print(accuracy_score(y1, y1_model), accuracy_score(y2, y2_model))

#this is tedius by hand, so you can use scikit-learns cross_val_score
from sklearn.model_selection import cross_val_score
print(cross_val_score(model, X, y, cv=5))

#train on all points but each point is a trial
#leace -on-out cross validation

from sklearn.model_selection import LeaveOneOut
scores = cross_val_score(model, X, y, cv=LeaveOneOut())
print(scores)
print(scores.mean())

#Selecting the best model
##Validation curves in scikit-learn
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline

def PolynomialRegression(degree=2, **kwargs):
    return make_pipeline(PolynomialFeatures(degree), LinearRegression(**kwargs))

#data generation
import numpy as np

def make_data(N, err=1.0, rseed=1):
    # randomly sample the data
    rng = np.random.RandomState(rseed)
    X = rng.rand(N, 1) ** 2
    y = 10 - 1. / (X.ravel() + 0.1)
    if err > 0:
        y += err * rng.randn(N)
    return X, y

X, y = make_data(40)

import matplotlib
matplotlib.use("TKagg")
import matplotlib.pyplot as plt
import seaborn; seaborn.set()  # plot formatting

X_test = np.linspace(-0.1, 1.1, 500)[:, None]

plt.scatter(X.ravel(), y, color='black')
axis = plt.axis()
for degree in [1, 3, 5]:
    y_test = PolynomialRegression(degree).fit(X, y).predict(X_test)
    plt.plot(X_test.ravel(), y_test, label='degree={0}'.format(degree))

plt.xlim(-0.1, 1.0)
plt.ylim(-2, 12)
plt.legend(loc='best')
plt.show()
plt.clf()

#lets look at the validation curve
from sklearn.model_selection import validation_curve
degree = np.arange(0, 21)
train_score, val_score = validation_curve(PolynomialRegression(),
                                                         X, y,
                                          'polynomialfeatures__degree',
                                          degree, cv=7)

plt.plot(degree, np.median(train_score, 1), color='blue', label='training score')
plt.plot(degree, np.median(val_score, 1), color='red', label='validation score')
plt.legend(loc='best')
plt.ylim(0, 1)
plt.xlabel('degree')
plt.ylabel('score');
plt.show()
plt.clf()

#3rd degree the best
plt.scatter(X.ravel(), y)
lim = plt.axis()
y_test = PolynomialRegression(3).fit(X, y).predict(X_test)
plt.plot(X_test.ravel(), y_test);
plt.axis(lim);
plt.show()
plt.clf()

#Learning curves
#generate some data
X2, y2 = make_data(200)
plt.scatter(X2.ravel(), y2);

degree = np.arange(21)
train_score2, val_score2 = validation_curve(PolynomialRegression(),
                                            X2, y2,
                                            'polynomialfeatures__degree',
                                            degree, cv=7)

plt.plot(degree, np.median(train_score2, 1), color='blue',
         label='training score')
plt.plot(degree, np.median(val_score2, 1), color='red',
         label='validation score')
plt.plot(degree, np.median(train_score, 1), color='blue',
         alpha=0.3, linestyle='dashed')
plt.plot(degree, np.median(val_score, 1), color='red',
         alpha=0.3, linestyle='dashed')
plt.legend(loc='lower center')
plt.ylim(0, 1)
plt.xlabel('degree')
plt.ylabel('score');

plt.show()
plt.clf

from sklearn.model_selection import learning_curve

fig, ax = plt.subplots(1, 2, figsize=(16, 6))
fig.subplots_adjust(left=0.0625, right=0.95, wspace=0.1)

for i, degree in enumerate([2, 9]):
    N, train_lc, val_lc = learning_curve(PolynomialRegression(degree),
                                         X, y, cv=7,
                                         train_sizes=np.linspace(0.3, 1, 25))

    ax[i].plot(N, np.mean(train_lc, 1), color='blue', label='training score')
    ax[i].plot(N, np.mean(val_lc, 1), color='red', label='validation score')
    ax[i].hlines(np.mean([train_lc[-1], val_lc[-1]]), N[0], N[-1],
                 color='gray', linestyle='dashed')

    ax[i].set_ylim(0, 1)
    ax[i].set_xlim(N[0], N[-1])
    ax[i].set_xlabel('training size')
    ax[i].set_ylabel('score')
    ax[i].set_title('degree = {0}'.format(degree), size=14)
    ax[i].legend(loc='best')
plt.show()
plt.clf()

#plotting a learning curve can help you desides if
#you need more data or a more complex model

#validationg in practice: Grid search

from sklearn.model_selection import GridSearchCV

param_grid = {'polynomialfeatures__degree': np.arange(21),
              'linearregression__fit_intercept': [True, False],
              'linearregression__normalize': [True, False]}

grid = GridSearchCV(PolynomialRegression(), param_grid, cv=7)
grid.fit(X, y)
print(grid.best_params_)

model = grid.best_estimator_

plt.scatter(X.ravel(), y)
lim = plt.axis()
y_test = model.fit(X,y).predict(X_test)
plt.plot(X_test.ravel(), y_test);
plt.axis(lim);
plt.show()
plt.clf()


import matplotlib.pyplot as plt
import numpy as np
import math

points = 50
stop = 2*math.pi
step =  stop / points
xpoints = np.arange(0,stop,step)

fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
ax1.set_ylabel("sin")
ax1.plot(xpoints, np.sin(xpoints))
ax2.set_ylabel("cos")
ax2.plot(xpoints, np.cos(xpoints))
plt.show()


from numpy import pi

def comp_inductance(self):
    """Compute the end winding inductance based on permeance coefficient stored in EndWindingRect object
    from "Design of Rotating Electrical Machines", J. Pyrhonen, second edition

    Parameters
    ----------
    self: EndWindingRect
        A EndWindingRect object

    Returns
    -------
    Lew : float
        end winding inductance [H].
    """

    if self.parent is not None:
        winding = self.parent
    else:
        return 0

    p = winding.p
    Ntcoil = winding.Ntcoil

    mu0 = 4 * pi * 1e-7

    if winding.Lewout is not None:
        l_ew = winding.Lewout
    else:
        l_ew = 0

    # Calculate width of end winding
    w_ew = 2 * self.comp_length()

    # Calculate Eq(4.100)
    l_lambda = 2 * l_ew * self.lambda_length + w_ew * self.lambda_width

    # Calculate end-winding inductance from Eq (4.98) p.261
    Lw = 2 / p * Ntcoil ** 2 * mu0 * l_lambda

    return Lw


import PCA as pc
import numpy as np
from sklearn.decomposition import PCA

if __name__ == "__main__":
    data = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])

    pca = pc.PCA(n_components=1)
    pca.fit(data,rowvar=False)
    res = pca.transform(data,rowvar=False)
    ratio = pca.variance_ratio(only=True)
    print("各特征的权重为: ratio = ",ratio)
    print("使用本库进行计算得到的PCA降维结果为: res = ", res)

    pca1 = PCA(n_components=1)
    res = pca1.fit_transform(data)
    ratio = pca1.explained_variance_ratio_
    print("各特征的权重为: ratio = ",ratio)
    print("使用sklearn.decomposition.PCA 验证的结果为: res = ", res)


"""
Source: https://www.kaggle.com/kelvin0815/titanic/first-trial-following-dataquest/run/79128
"""
import os
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import KFold
from sklearn import cross_validation
from modeldb.sklearn_native.ModelDbSyncer import *
from modeldb.sklearn_native import SyncableRandomSplit
from modeldb.sklearn_native import SyncableMetrics

ROOT_DIR = '../../../../server/'
DATA_PATH = '../../../../data/'

name = "test1"
author = "author"
description = "kaggle-titanic-script"
# Creating a new project
syncer_obj = Syncer(
    NewOrExistingProject(name, author, description),
    NewOrExistingExperiment("expName", "expDesc"),
    NewExperimentRun("titanic test"))

# Read the training set csv file.
# Note: This dataset is not included in the repo because of Kaggle
# restrictions.
# It can be downloaded from https://www.kaggle.com/c/titanic/data
titanic = pd.read_csv_sync(DATA_PATH + 'titanic-train.csv')

# =====================Preprocessing the data=====================
# Fill the missing value in "Age".
titanic["Age"] = titanic["Age"].fillna(titanic["Age"].median())

# Converting the Sex Column to numeric value
titanic.loc[titanic["Sex"] == "male", "Sex"] = 0
titanic.loc[titanic["Sex"] == "female", "Sex"] = 1
# Converting the Embarked Column
titanic["Embarked"] = titanic["Embarked"].fillna("S")
titanic.loc[titanic["Embarked"] == "S", "Embarked"] = 0
titanic.loc[titanic["Embarked"] == "C", "Embarked"] = 1
titanic.loc[titanic["Embarked"] == "Q", "Embarked"] = 2

# NOTE: .loc commands don't create a new dataframe id
# =====================Record the prediction======================
# Making predictions with Linear Regression
predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]
alg = LinearRegression()
kf = KFold(titanic.shape[0], n_folds=3, random_state=1)
predictions = []

for train, test in kf:
    train_predictors = (titanic[predictors].iloc[train, :])
    train_target = titanic["Survived"].iloc[train]
    alg.fit_sync(train_predictors, train_target)
    test_predictions = alg.predict_sync(titanic[predictors].iloc[test, :])
    predictions.append(test_predictions)

# Evaluating error and accuracy
predictions = np.concatenate(predictions, axis=0)
predictions[predictions > .5] = 1
predictions[predictions <= .5] = 0

accuracy = 1 - sum(abs(predictions - titanic["Survived"])) / len(predictions)

print(accuracy)

# Logistic Regression
alg = LogisticRegression(random_state=1)

scores = cross_validation.cross_val_score_sync(
    alg, titanic[predictors], titanic["Survived"], cv=3)
print scores.mean()
syncer_obj.sync()


#!/usr/bin/python
"""
This is a mixture of two gaussians example

We mode the proportion of one distribution with a random variable.

For the sample error it would be nice to use:

precision = Gamma('precision', alpha=0.1, beta=0.1

as it is the conjugate prior of the precision.

However, this produced odd results.  In order to get this example to
work properly I had to modify the Uniform range for the precision parameter
until the results were reasonable.  Oddly enough increasing this range
will decrease DIC (good), but the visible fit is worse.

"""

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import pymc as mc

## example from elem. stat. learning book
book_mu1 = 4.62
book_mu2 = 1.06
book_var1 = 0.87
book_var2 = 0.77
book_pi = 0.546

x1 = np.array([-0.39,0.12,0.94,1.67,1.76,2.44,3.72,4.28,4.92,5.53])
x2 = np.array([ 0.06,0.48,1.01,1.68,1.80,3.25,4.12,4.60,5.28,6.22])
x  = np.hstack([x1,x2])

## the mixing parameter
pi = mc.Uniform('pi',lower=0,upper=1)
delta = mc.Bernoulli( "delta",p=pi,size=x.size) # produces 1 with proportion p.

## other model parameters
sigma = mc.Uniform("sigma", 0.0,0.5)
var1 = mc.Uniform('var1',0,1)
var2 = mc.Uniform('var2',0,1)
mode1 = mc.Normal("mode1",mu=0,tau=var1**-2)
mode2 = mc.Normal("mode2",mu=0,tau=var2**-2)

@mc.deterministic(plot=False)
def mix_model(delta=delta,mode1=mode1,mode2=mode2):
    return (1.0-delta)*mode1 + delta*mode2

obs = mc.Normal("obs",mu=mix_model,tau=sigma**-2,value=x,observed=True)
model = mc.Model({"mode1":mode1,"mode2":mode2,"sigma":sigma,"pi":pi,
                  "var1":var1,"var2":var2,"obs":obs})

M = mc.MCMC(model)
M.sample(100000,50000)
mc.Matplot.plot(M)

## relabel the estimates if necessary
if M.trace('mode1')[:].mean() > M.trace('mode2')[:].mean():
    est_mu1  = M.trace('mode1')[:].mean()
    est_var1 = M.trace('var1')[:].mean()
    est_mu2  = M.trace('mode2')[:].mean()
    est_var2 = M.trace('var2')[:].mean()
    est_pi   = M.trace('pi')[:].mean()
else:
    est_mu1  = M.trace('mode2')[:].mean()
    est_var1 = M.trace('var2')[:].mean()
    est_mu2  = M.trace('mode1')[:].mean()
    est_var2 = M.trace('var1')[:].mean()
    est_pi   = 1.0 - M.trace('pi')[:].mean()

print 'book,model'
print 'mu1', book_mu1,est_mu1
print 'mu2', book_mu2,est_mu2
print 'var1', book_var1,est_var1
print 'var2', book_var2,est_var2
print 'pi', book_pi, est_pi
print 'DIC',M.dic

fig = plt.figure(1)
ax = fig.add_subplot(111)
n,bins,patches = ax.hist(x,15,normed=1,facecolor='gray',alpha=0.75)

p1 = mlab.normpdf(bins, book_mu1, np.sqrt(book_var1))
p2 = mlab.normpdf(bins, book_mu2, np.sqrt(book_var2))
l1 = plt.plot(bins, p1, 'r--', linewidth=1)
l2 = plt.plot(bins, p2, 'r--', linewidth=1)

## add a 'best fit' line (results from here)
p3 = mlab.normpdf(bins, est_mu1, np.sqrt(est_var1))
p4 = mlab.normpdf(bins, est_mu2, np.sqrt(est_var2))
l3 = plt.plot(bins, p3, 'k-', linewidth=1)
l4 = plt.plot(bins, p4, 'k-', linewidth=1)

plt.xlabel('x')
plt.ylabel('freq')
plt.ylim([0,0.8])

ax.legend((l1[0], l3[0]), ('Book Estimate', 'My Estimate') )
#plt.savefig('../TwoComponentGauss.png')
plt.show()


"""
This Python3 script creates a Pythagoras tree in SVG or PNG format

Requires numpy and matplotlib to create pngs.

Get started with : python Pythagoras_tree.py --help
"""
import argparse
from math import atan2, ceil, pi, sqrt
from typing import List

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm, patches

def pythagoras_tree(ratio: float = 1.0, nb_levels: int = 12):
    """
    Compute Pythagoras_tree
    The Pythagoras Tree is a plane fractal constructed from squares.
    It is named after Pythagoras because each triple of touching squares
    encloses a right triangle, in a configuration traditionally used to
    depict the Pythagorean theorem.
    http://en.wikipedia.org/wiki/Pythagoras_tree

          All these arguments are optional: the function can run with
          argument.
    Output :
          - Matrix M: Pythagoras tree is stored in a matrix M.
            This matrix has 5 columns.
            Each row corresponds to the coordinate of each square of the tree
            The two first columns give the bottom-left position of each
            square. The third column corresponds to the orientation angle of
            each square. The fourth column gives the size of each square. The
            fifth column specifies the level of recursion of each square.
            The first row corresponds to the root of the tree. It is always
            the same
            M[0,:] = [0 -1 0 1 1];
            The leaf located at row i will give 2 leaves located at 2*i and
            2*i+1.
    """
    # pylint: disable=too-many-locals
    # Check inputs
    if ratio <= 0:
        raise Exception("Length of ratio has to be greater than zero")
    if int(nb_levels) != float(nb_levels):
        raise Exception("The number of level has to be integer")

    # Compute constants
    c_d = sqrt(1.0 + ratio ** 2)
    # Normalized length 1
    c_1 = 1.0 / c_d
    # Normalized length 2
    c_2 = ratio / c_d
    # Translation pattern
    tr_pat = np.array(
        [[0.0, 1.0 / (1.0 + ratio ** 2)], [1.0, 1.0 + ratio / (1.0 + ratio ** 2)]]
    )
    # Defines the first rotation angle
    alpha1 = atan2(ratio, 1.0)
    # Defines the second rotation angle
    alpha2 = alpha1 - pi / 2.0
    # Number of elements (square)
    nb_elements = 2 ** (nb_levels + 1) - 1
    # Matrice containing the tree
    pt_arrray = np.zeros((nb_elements, 5))
    # Initialization of the tree
    pt_arrray[0, :] = [0.0, -1.0, 0.0, 1.0, 1.0]

    # Compute the level of each square contained in the resulting matrix
    offset = 0
    for i in range(nb_levels + 1):
        tmp = 2 ** i
        pt_arrray[offset : offset + tmp, 4] = i
        offset += tmp

    def mat_rot(angle_rad: float) -> np.ndarray:
        c_a = np.cos(angle_rad)
        s_a = np.sin(angle_rad)
        return np.array([[c_a, -s_a], [s_a, c_a]])

    # Compute the position and size of each square wrt its parent
    for i in range(1, nb_elements, 2):
        j = (i + 1) // 2 - 1
        t_m = pt_arrray[j, 3] * mat_rot(pt_arrray[j, 2]) @ tr_pat
        t_x = t_m[0, :] + pt_arrray[j, 0]
        t_y = t_m[1, :] + pt_arrray[j, 1]
        theta1 = (pt_arrray[j, 2] + alpha1) % (2.0 * pi)
        theta2 = (pt_arrray[j, 2] + alpha2) % (2.0 * pi)
        pt_arrray[i, 0:4] = [t_x[0], t_y[0], theta1, pt_arrray[j, 3] * c_1]
        pt_arrray[i + 1, 0:4] = [t_x[1], t_y[1], theta2, pt_arrray[j, 3] * c_2]
    return pt_arrray

def _svg_write_metadata(write):
    write("\t<title>Pythagoras tree</title>\n")
    write("\t<metadata>\n")
    write("\t\t<rdf:RDF>\n")
    write("\t\t\t<cc:Work\n")
    write('\t\t\t\trdf:about="">\n')
    write("\t\t\t\t<dc:format>image/svg+xml</dc:format>\n")
    write("\t\t\t\t<dc:type\n")
    write("\t\t\t\t\trdf:resource=" + '"http://purl.org/dc/dcmitype/StillImage"/>\n')
    write("\t\t\t\t<dc:title>Pythagoras tree</dc:title>\n")
    write("\t\t\t\t<dc:creator>\n")
    write("\t\t\t\t\t<cc:Agent>\n")
    write("\t\t\t\t\t\t<dc:title>Guillaume Jacquenot</dc:title>\n")
    write("\t\t\t\t\t</cc:Agent>\n")
    write("\t\t\t\t</dc:creator>\n")
    write("\t\t\t\t<cc:license\n")
    write(
        "\t\t\t\t\t\trdf:resource="
        + '"http://creativecommons.org/licenses/by-nc-sa/3.0/"/>\n'
    )
    write("\t\t\t</cc:Work>\n")
    write("\t\t\t<cc:License\n")
    write(
        "\t\t\t\trdf:about=" + '"http://creativecommons.org/licenses/by-nc-sa/3.0/">\n'
    )
    write("\t\t\t\t<cc:permits\n")
    write(
        "\t\t\t\t\trdf:resource=" + '"http://creativecommons.org/ns#Reproduction"/>\n'
    )
    write("\t\t\t\t<cc:permits\n")
    write(
        "\t\t\t\t\trdf:resource=" + '"http://creativecommons.org/ns#Reproduction"/>\n'
    )
    write("\t\t\t\t<cc:permits\n")
    write(
        "\t\t\t\t\trdf:resource=" + '"http://creativecommons.org/ns#Distribution"/>\n'
    )
    write("\t\t\t\t<cc:requires\n")
    write("\t\t\t\t\trdf:resource=" + '"http://creativecommons.org/ns#Notice"/>\n')
    write("\t\t\t\t<cc:requires\n")
    write("\t\t\t\t\trdf:resource=" + '"http://creativecommons.org/ns#Attribution"/>\n')
    write("\t\t\t\t<cc:prohibits\n")
    write(
        "\t\t\t\t\trdf:resource=" + '"http://creativecommons.org/ns#CommercialUse"/>\n'
    )
    write("\t\t\t\t<cc:permits\n")
    write(
        "\t\t\t\t\trdf:resource="
        + '"http://creativecommons.org/ns#DerivativeWorks"/>\n'
    )
    write("\t\t\t\t<cc:requires\n")
    write("\t\t\t\t\trdf:resource=" + '"http://creativecommons.org/ns#ShareAlike"/>\n')
    write("\t\t\t</cc:License>\n")
    write("\t\t</rdf:RDF>\n")
    write("\t</metadata>\n")

def pythagor_tree_write2svg(
    pt_array: np.ndarray,
    ratio: float = 0.8,
    nb_levels: int = 12,
    colormap_name: str = "summer",
):
    """
    A svg file giving a vectorial display of the tree. The name of
    file is generated from the parameter m, n, colormap. The file is
    stored in the current folder.
    """
    # pylint: disable=too-many-locals
    display_metadata = True

    nb_elements = pt_array.shape[0]
    length_offset = pt_array[nb_elements - 1, 3] + 0.1
    min_x = np.min(pt_array[:, 0] - sqrt(2) * pt_array[:, 3]) - length_offset
    max_x = np.max(pt_array[:, 0] + sqrt(2) * pt_array[:, 3]) + length_offset
    min_y = np.min(pt_array[:, 1]) - length_offset
    max_y = np.max(pt_array[:, 1] + sqrt(2) * pt_array[:, 3]) + length_offset

    # Compute the color of tree
    colormap = cm.get_cmap(colormap_name)
    nb_pixels = 100
    fig_w = ceil(nb_pixels * (max_x - min_x))
    fig_h = ceil(nb_pixels * (max_y - min_y))
    filename = (
        "Pythagoras_tree_"
        + str(ratio).replace(".", "_")
        + "__"
        + str(nb_levels)
        + "__"
        + colormap_name
        + ".svg"
    )
    with open(filename, "wt", encoding="utf-8") as fid:
        write = fid.write
        write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
        if not display_metadata:
            write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n')
            write('  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n')
        write('<svg width="{0}" height="{1}" version="1.1"\n'.format(fig_w, fig_h))
        # w(['<svg width="12cm" height="4cm" version="1.1"\n']); % Wfig,

        # w(['<svg width="15cm" height="10cm" '...
        #              'viewBox="0 0 %d %d" version="1.1"\n'],...
        #              Wfig,Hfig);
        if display_metadata:
            write('\txmlns:dc="http://purl.org/dc/elements/1.1/"\n')
            write('\txmlns:cc="http://creativecommons.org/ns#"\n')
            write('\txmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n')
        write('\txmlns:svg="http://www.w3.org/2000/svg"\n')
        write('\txmlns="http://www.w3.org/2000/svg"\n')
        write('\txmlns:xlink="http://www.w3.org/1999/xlink">\n')

        if display_metadata:
            _svg_write_metadata(write)
        write("\t<defs>\n")
        write('\t\t<rect width="{0}" height="{0}" \n'.format(nb_pixels))
        write('\t\t\tx="0" y="0"\n')
        write('\t\t\tstyle="fill-opacity:1;stroke:#00d900;stroke-opacity:1"\n')
        write('\t\t\tid="squa"\n')
        write("\t\t/>\n")
        write("\t</defs>\n")
        write(
            '\t<g transform="translate({0} {1}) rotate(180) " >\n'.format(
                round(nb_pixels * max_x), round(nb_pixels * max_y)
            )
        )
        for i in range(nb_levels + 1):
            write(
                '\t\t<g style="fill:{0};" >\n'.format(
                    matplotlib.colors.rgb2hex(
                        colormap(1.0 - float(i) / (nb_levels + 1))
                    )
                )
            )
            offset = 2 ** i - 1
            for j in range(2 ** i):
                k = j + offset
                write(
                    (
                        '\t\t\t<use xlink:href="#squa" '
                        + 'transform="translate({0:+010.5f} {1:+010.5f}) '
                        + 'rotate({2:3.1f}) scale({3:8.6f})" />\n'
                    ).format(
                        nb_pixels * pt_array[k, 0],
                        nb_pixels * pt_array[k, 1],
                        pt_array[k, 2] * 180 / pi,
                        pt_array[k, 3],
                    )
                )
            write("\t\t</g>\n")
        write("\t</g>\n")
        write("</svg>\n")

def pythagor_tree_plot(
    pt_array: np.ndarray, colormap_name: str = "summer", output_filename: str = "lm.png"
):
    """Plot a Pythagoras tree for a PNG format"""
    colormap = cm.get_cmap(colormap_name)
    fig, axis = plt.subplots()
    for i in range(pt_array.shape[0]):
        c_x = pt_array[i, 0]
        c_y = pt_array[i, 1]
        theta = pt_array[i, 2] * 180.0 / pi
        s_i = pt_array[i, 3]
        rect = patches.Rectangle(
            [c_x, c_y],
            s_i,
            s_i,
            angle=theta,
            ec="none",
            color=colormap(1.0 - i / (pt_array[-1, 4] + 1)),
        )
        axis.add_patch(rect)
    plt.xlim([-4, 4])
    plt.ylim([-1.5, 3.5])
    # plt.gca().relim()
    plt.gca().set_aspect("equal", adjustable="box")
    plt.axis("off")
    fig.savefig(output_filename, bbox_inches="tight", dpi=300)

def get_list_of_colormaps() -> List[str]:
    """Return the list of avalaible colors"""
    return [
        "autumn",
        "bone",
        "colorcube",
        "cool",
        "copper",
        "flag",
        "gray",
        "hot",
        "hsv",
        "jet",
        "lines",
        "pink",
        "prism",
        "spring",
        "summer",
        "white",
        "winter",
    ]

def is_colormap(cmap: str) -> bool:
    """This function returns True if 'cmap' is a valid colormap"""
    return cmap in get_list_of_colormaps()

def main():
    """Main entrypoint"""
    parser = argparse.ArgumentParser(
        description="This script creates a SVG image of a Pythagoras tree, a"
        + " plane fractal constructed from squares."
    )
    parser.add_argument(
        "-r",
        "--ratio",
        type=float,
        default=1.0,
        help="r ( r > 0 ) is the relative length of one of the side "
        + "right-angled triangle. "
        + "The second side of the right-angle is taken to be one. "
        + "To have a symmetric tree, r has to be 1.",
    )
    parser.add_argument(
        "-l",
        "--level",
        type=int,
        default=10,
        help="n is the level of recursion. The number of elements of "
        + "tree is equal to 2**(n+1)-1. A reasonable number for n "
        + "is 10.",
    )
    parser.add_argument(
        "-p",
        "--plot",
        action="store_true",
        help="Option used to display the tree as a PNG image with matplotlib",
    )
    parser.add_argument(
        "-c",
        "--colormap",
        type=str,
        default="summer",
        help="Matplotlib colormap used to generate color of the different "
        + "levels of the tree. "
        + "Possible values are {0}".format(" ".join(get_list_of_colormaps())),
    )
    args = parser.parse_args()

    # Create a matrix containing the informations representing the tree
    # Each row represents a single square
    pt_array = pythagoras_tree(ratio=args.ratio, nb_levels=args.level)

    if args.plot:
        # Display the tree
        filename = (
            "Pythagoras_tree_"
            + str(args.ratio).replace(".", "_")
            + "__"
            + str(args.level)
            + "__"
            + args.colormap
            + ".png"
        )
        pythagor_tree_plot(
            pt_array, colormap_name=args.colormap, output_filename=filename
        )

    # Write results to an SVG file
    pythagor_tree_write2svg(pt_array, args.ratio, args.level, args.colormap)

if __name__ == "__main__":
    main()


import numpy as np # pip install numpy

∂ = array_like = array<numpy> | scalar<numpy | python> | object<PEP 3118 buffer interface>
	object<__array_struct__ | __array_interface__> | object<__array__ function>
	object<list of lists> | zero_dimensional_array< object<dtype> >

	zero_dimensional_array = scalar
		zda = np.array(1)
		zda.shape # ()
		zda.ndim  # 0

np.Inf | Infinity | PINF | infty
np.NAN
np.pi

np.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
np.arange(?start, stop, ?step, ?dtype=None, *, ?like=None)
np.zeros(shape=int|(int..), ?dtype=np.float64, *, ?like='C|F')
np.ones(↑..)
np.empty(↑..)
np.reshape(a=∂, newshape=int|(int..), order='C|F|A')
np.split(ary, indices_or_sections, axis=0)
np.split_array(↑..)
np.concatenate((a1, a2, ...), ?axis=0, ?out=None|ndarray, ?dtype=None|''|dtype, ?casting='same_kind|no|equiv|safe|unsafe')
np.diff(a=∂, ?n=1, ?axis=-1, ?prepend=∂, ?append=∂)
np.where()
np.allclose(a=∂, b=∂, rtol=1e-05, atol=1e-08, equal_nan=False)
np.shape()
np.flatten(?c='C|F|A|K')
np.append(arr=∂, values=∂, ?axis=None|int)

np.random.uniform(?low=0.0, ?high=1.0, ?size=None|int|(int..))
np.round_(a=[], ?decimals=0, ?out=None)

np.multiply()
np.ceil()
np.sum()
np.mean()
np.mean()
np.average()
np.std()
np.var()
np.matmul(a,b)
np.cov()
np.percentile()
np.quantile()
np.amin()
np.amax()

a = np.array() # `array` type is alies for `ndarray`

a.base
a.data
a.dtype
a.flags
a.flat        []
a.imag
a.itemset
a.itemsize    int # byte-size
a.nbytes
a.ndim        int
a.real
a.shape       (int,..)
a.size        int
a.T           []

a.all()
a.any()
a.argmax()
a.argmin()
a.argpartition()
a.argsort()
a.astype()
a.byteswap()
a.choose()
a.clip()
a.compress()
a.conj()
a.conjugate()
a.copy()
a.ctypes
a.cumprod()
a.cumsum()
a.diagonal()
a.dot()
a.dump()
a.dumps()
a.fill()
a.flatten(?order='C|F|A|K')
a.getfield()
a.item()
a.max()
a.mean()
a.min()
a.newbyteorder()
a.nonzero()
a.partition()
a.prod()
a.ptp()
a.put()
a.ravel()
a.repeat()
a.reshape()
a.resize()
a.round()
a.searchsorted()
a.setfield()
a.setflags()
a.sort()
a.squeeze()
a.std()
a.strides()
a.sum()
a.swapaxes()
a.take()
a.tobytes()
a.tofile()
a.tolist()
a.tostring() # deprecated
a.trace()
a.transpose()
a.var()
a.view()


'''

Machine Learning - Normal Data Distribution
Normal Data Distribution

In the previous chapter we learned how to create a completely random array, of a given size, and between two given values.

In this chapter we will learn how to create an array where the values are concentrated around a given value.

In probability theory this kind of data distribution is known as the normal data distribution, or the Gaussian data distribution, after the mathematician Carl Friedrich Gauss who came up with the formula of this data distributio
'''

import matplotlib.pyplot as plt
import numpy as np

speed=np.random.normal(5.0,1.0,100000)
plt.hist(speed,100)
print(speed)
plt.show()

'''

Note: A normal distribution graph is also known as the bell curve because of it's characteristic shape of a bell.
Histogram Explained

We use the array from the numpy.random.normal() method, with 100000 values,  to draw a histogram with 100 bars.

We specify that the mean value is 5.0, and the standard deviation is 1.0.

Meaning that the values should be concentrated around 5.0, and rarely further away than 1.0 from the mean.

And as you can see from the histogram, most values are between 4.0 and 6.0, with a top at approximately 5.0.
'''

'''

Machine Learning - Scatter Plot
Scatter Plot

A scatter plot is a diagram where each value in the data set is represented by a dot.

The Matplotlib module has a method for drawing scatter plots, it needs two arrays of the same length, one for the values of the x-axis, and one for the values of the y-axis:

x = [5,7,8,7,2,17,2,9,4,11,12,9,6]

y = [99,86,87,88,111,86,103,87,94,78,77,85,86]

The x array represents the age of each car.

The y array represents the speed of each car.
'''


"""
Approximation of functions by linear combination of basis functions in
function spaces and the least squares method (or the Galerkin method).
2D version.
"""
import sympy as sym
import numpy as np

def least_squares(f, psi, Omega, symbolic=True, print_latex=False):
    """
    Given a function f(x,y) on a rectangular domain
    Omega=[[xmin,xmax],[ymin,ymax]],
    return the best approximation to f(x,y) in the space V
    spanned by the functions in the list psi.
    """
    N = len(psi) - 1
    A = sym.zeros(N+1, N+1)
    b = sym.zeros(N+1, 1)
    x, y = sym.symbols('x y')
    print('...evaluating matrix...')
    for i in range(N+1):
        for j in range(i, N+1):
            print(('(%d,%d)' % (i, j)))

            integrand = psi[i]*psi[j]
            if symbolic:
                I = sym.integrate(integrand,
                                 (x, Omega[0][0], Omega[0][1]),
                                 (y, Omega[1][0], Omega[1][1]))
            if not symbolic or isinstance(I, sym.Integral):
                # Could not integrate symbolically, use numerical int.
                print(('numerical integration of', integrand))
                integrand = sym.lambdify([x,y], integrand)
                I = sym.mpmath.quad(integrand,
                                   [Omega[0][0], Omega[0][1]],
                                   [Omega[1][0], Omega[1][1]])
            A[i,j] = A[j,i] = I
        integrand = psi[i]*f
        if symbolic:
            I = sym.integrate(integrand,
                             (x, Omega[0][0], Omega[0][1]),
                             (y, Omega[1][0], Omega[1][1]))
        if not symbolic or isinstance(I, sym.Integral):
            # Could not integrate symbolically, use numerical int.
            print(('numerical integration of', integrand))
            integrand = sym.lambdify([x,y], integrand)
            I = sym.mpmath.quad(integrand,
                               [Omega[0][0], Omega[0][1]],
                               [Omega[1][0], Omega[1][1]])
        b[i,0] = I
    print()
    print(('A:\n', A, '\nb:\n', b))
    if symbolic:
        c = A.LUsolve(b)  # symbolic solve
        # c is a sympy Matrix object, numbers are in c[i,0]
        c = [c[i,0] for i in range(c.shape[0])]
    else:
        c = sym.mpmath.lu_solve(A, b)  # numerical solve
    print(('coeff:', c))

    u = sum(c[i]*psi[i] for i in range(len(psi)))
    print(('approximation:', u))
    print(('f:', sym.expand(f)))
    if print_latex:
        print((sym.latex(A, mode='plain')))
        print((sym.latex(b, mode='plain')))
        print((sym.latex(c, mode='plain')))
    return u

def comparison_plot(f, u, Omega, plotfile='tmp', title=''):
    """Compare f(x,y) and u(x,y) for x,y in Omega in a plot."""
    x, y = sym.symbols('x y')

    f = sym.lambdify([x,y], f, modules="numpy")
    u = sym.lambdify([x,y], u, modules="numpy")
    # When doing symbolics, Omega can easily contain symbolic expressions,
    # assume .evalf() will work in that case to obtain numerical
    # expressions, which then must be converted to float before calling
    # linspace below
    for r in range(2):
        for s in range(2):
            if not isinstance(Omega[r][s], (int,float)):
                Omega[r][s] = float(Omega[r][s].evalf())

    resolution = 41  # no of points in plot
    xcoor = np.linspace(Omega[0][0], Omega[0][1], resolution)
    ycoor = np.linspace(Omega[1][0], Omega[1][1], resolution)
    xv, yv = np.meshgrid(xcoor, ycoor)
    # Vectorized functions expressions does not work with
    # lambdify'ed functions without the modules="numpy"
    exact  = f(xv, yv)
    approx = u(xv, yv)
    error = exact - approx

    import matplotlib.pyplot as plt
    plt.figure()
    contours = plt.contour(xv, yv, error, 8)     # 8 contour lines
    plt.clabel(contours, inline=1, fontsize=10)  # labels
    if title: plt.title(title)
    if plotfile:
        plt.savefig('%s_error_c.pdf' % plotfile)
        plt.savefig('%s_error_c.png' % plotfile)
    fig = plt.figure()
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    #ax = fig.add_subplot(111) #, projection='3d')
    ax = fig.gca(projection='3d')
    surf = ax.plot_surface(xv, yv, error, rstride=1, cstride=1,
                           cmap=cm.coolwarm, linewidth=0,
                           antialiased=False)
    fig.colorbar(surf, shrink=0.5, aspect=5)
    if title: plt.title(title)
    if plotfile:
        plt.savefig('%s_error_s.pdf' % plotfile)
        plt.savefig('%s_error_s.png' % plotfile)
    plt.show()

if __name__ == '__main__':
    print('Module file not meant for execution.')


from app.utils.methods import BaseMethod
from sympy import Symbol
from sympy.parsing.sympy_parser import parse_expr

class Bisection(BaseMethod):

    def __init__(self, x0, x1, tolerance, iterations, function, **kwargs):
        self.xl = x0
        self.xh = x1
        self.tol = tolerance
        self.iter = iterations
        self.function = function
        self.array = []

    def run(self):
        f = parse_expr(self.function)
        x = Symbol("x")
        fxl = f.subs(x, self.xl)
        fxh = f.subs(x, self.xh)

        if fxl == 0:
            return self.success_response(f"{self.xl} is a root")
        elif fxh == 0:
            return self.success_response(f"{self.xh} is a root")
        elif fxl * fxh > 0:
            return self.failed_response("The interval does not have a root")
        else:
            error = self.tol + 1
            xm = (self.xl + self.xh) / 2
            self.array.append(["i", "x0", "x1", "xm", "f(xm)", "error"])
            for i in range(self.iter):
                fxm = f.subs(x, xm)

                if fxm == 0 or error <= self.tol:
                    break

                if fxl * fxm < 0:
                    self.xh = xm
                else:
                    self.xl = xm

                x_aux = xm
                xm = (self.xl + self.xh) / 2
                error = abs(xm - x_aux)
                self.array.append([str(i), str(self.xl), str(xm), str(self.xh), str(fxm), str(error)])

            if fxm == 0:
                return {
                    "method_status": "success",
                    "result": f"{xm} is a root",
                    "iterations": self.array
                }
            elif error < self.tol:
                return {
                    "method_status": "success",
                    "result": f"{xm} approaches a root of the function with a tolerance of {self.tol}",
                    "iterations": self.array
                }
            else:
                return {
                    "method_status": "failed",
                    "result": "Max interactions exceeded",
                    "iterations": self.array
                }


# -*- coding: utf-8 -*-
"""
Created on Tue May 14 07:56:24 2019

@author: 08475268951
"""

import numpy as np

def f(x):
    return x**2 - np.pi

def bisseccao(f,a,b,tolerancia):
    tolerancia = 1e-10
    if np.abs(f(a)) < tolerancia:
        return a
    elif np.abs(f(b)) < tolerancia:
        return b
    elif f(a)*f(b) > 0:
        return np.Inf
    else:
        x = (a+b)/2
        print("x = {}".format(x))
        print("f(x) = {}".format(f(x)))
        nbiter = 0
        while np.abs(f(x)) >  tolerancia:
            if f(x)*f(a) > 0:
                a = x
            else:
                b = x
            x = (a+b)/2
            print("x = {}".format(x))
            print("f(x) = {}".format(f(x)))
            nbiter = nbiter + 1
        return x, nbiter


# Modern Computer Algebra

from Structures.QuotientFiniteField import QuotientFiniteField
from sage.matrix.all import Matrix

def berlekamp1_3(f, R):
    # paso 1
    q = R.get_domain().get_order()
    x = R.get_variable()
    n = R.degree(f)
    assert (q % 2 != 0)
    QF = QuotientFiniteField(R, f)
    xq = QF.repeated_squaring(x, q)

    # paso 2
    Q = []
    acum = R.one()
    for i in range(0, n):
        al = acum.list()
        zero = [R.get_domain().zero()] * (n - len(al))
        al.extend(zero)
        #al.reverse()
        Q.append(al)
        acum = QF.mul(xq, acum)

    # paso 3
    Q = Matrix(Q)
    I = Matrix.identity(n)
    QI = Q - I
    kqi = QI.kernel().basis_matrix().rows()
    r = len(kqi)
    bl = []
    for ro in kqi:
        # cuidao
        #ro.reverse()
        bl.append(R.get_true_value()(ro.list()))
    return r, bl

def berlekamp4_7(f, R, r, bl):
    q = R.get_domain().get_order()
    QF = QuotientFiniteField(R, f)
    if r == 1:
        return f

    # paso 4
    cl = []
    for i in range(0, r):
        cl.append(R.get_domain().get_random())
    al = [cl[k] * bl[k] for k in range(0, r)]
    a = R.zero()
    for a_ in al:
        a = R.add(a, a_)

    a = R.get_true_value()(a)

    # paso 5
    g1 = R.gcd(a, f)
    if g1 != R.one() and g1 != f:
        return g1

    # paso 6
    exp = (q - 1)//2
    b = QF.repeated_squaring(a, exp)

    # paso 7
    g2 = R.gcd(R.sub(b, R.one()), f)
    if g2 != R.one() and g2 != f:
        return g2
    else:
        return None

# f in R, R FiniteFieldPolynomial, k in Z
def berlekamp_k_times(f, R, k):
    # r = dimension of the basis of the berlekamp algebra
    # bl = basis of it
    r, bl = berlekamp1_3(f, R)
    if r == 1:
        return f
    for i in range(0, k):
        p = berlekamp4_7(f, R, r, bl)
        if p is not None:
            return p
    return None

# f in R, R FiniteFieldPolynomial, k in Z
def berlekamp_full(f, R, k):
    g = berlekamp_k_times(f, R, k)
    if g is None:
        return [f]
    elif g == f:
        return [f]
    gl = berlekamp_full(g, R, k)
    fl = berlekamp_full(R.quo(f, g), R, k)
    gl.extend(fl)
    return gl


from __future__ import division
import numpy as np

def cheb(N):
	x =  np.cos((np.pi/N)*np.linspace(0,N,N+1))
	x.shape = (N+1,1)
	lin = np.linspace(0,N,N+1)
	lin.shape = (N+1,1)

	c = np.ones((N+1,1))
	c[0], c[-1] = 2., 2.
	c = c*(-1.)**lin
	X = x*np.ones(N+1) # broadcast along 2nd dimension (columns)

	dX = X - X.T
	# print " x = \n", x, "\n"
	# print " c = \n", c, "\n"
	# print " X = \n", X, "\n"
	# print " dX = \n", dX, "\n"

	D = (c*(1./c).T)/(dX + np.eye(N+1))
	D  = D - np.diag(np.sum(D.T,axis=0))
	x.shape = (N+1,)
	return D, x

def cheb_loop(N):
	def p(j1):
		if (j1==0 or j1 == N): return 2.
		else: return 1.

	x = np.cos(np.pi*np.arange(N+1)/N)
	D = np.zeros((N+1,N+1))
	# j represents column index
	for j in range(0,N+1):
		for i in range(0,j)+range(j+1,N+1):
			D[i,j] = ((-1.)**(i+j))*p(i)/( p(j)*(x[i]- x[j]) )

	# Values on the main diagonal
	for j in xrange(1,N):
		D[j,j] = -x[j]/(2.*(1-x[j]**2.))

	D[0,0] = (1.+2.*N**2.)/6.
	D[N,N] = -(1.+2.*N**2.)/6.

	return D,x

def cheb_vectorized(N):
	x =  np.cos((np.pi/N)*np.linspace(0,N,N+1))
	x.shape = (N+1,1)
	lin = np.linspace(0,N,N+1)
	lin.shape = (N+1,1)

	c = np.ones((N+1,1))
	c[0], c[-1] = 2., 2.
	c = c*(-1.)**lin
	X = x*np.ones(N+1) # broadcast along 2nd dimension (columns)

	dX = X - X.T

	D = (c*(1./c).T)/(dX + np.eye(N+1))
	D  = D - np.diag(np.sum(D.T,axis=0))
	x.shape = (N+1,)
	# Here we return the differentation matrix and the Chebychev points,
	# numbered from x_0 = 1 to x_N = -1
	return D, x


import spinsim
import math
import numpy as np
import matplotlib.pyplot as plt
import datetime as dtm

time_now_string = dtm.datetime.now().strftime("%Y%m%dT%H%M%S")

def gaussian_pulse(time, modifier, pulse):
    pulse[0] = (math.pi/math.sqrt(math.tau))*math.exp(-0.5*(time**2))/math.tau
    pulse[1] = 0.0
    pulse[2] = 0.0

def cumulative_gaussian(t):
    return 0.5*(1 + math.erf(t/math.sqrt(2.0)))

plt.figure()
pulse_sample = np.empty(3, np.float64)

time_continuous = np.arange(-5.0, 5.0005, 1e-3)
pulse_continuous = []
for time_sample in time_continuous:
    gaussian_pulse(time_sample, 0, pulse_sample)
    pulse_continuous += [pulse_sample[0]]
pulse_continuous = np.asarray(pulse_continuous)
plt.plot(time_continuous, pulse_continuous, "k-")

time_step = 0.25
time_midpoint = 0.5*time_step + np.arange(-5.0, 5.0, time_step)
pulse_midpoint = []
for time_sample in time_midpoint:
    gaussian_pulse(time_sample, 0, pulse_sample)
    pulse_midpoint += [pulse_sample[0]]
pulse_midpoint = np.asarray(pulse_midpoint)
plt.plot(time_midpoint, pulse_midpoint, "bo")

time_quadrature = []
pulse_quadrature = []
for time_sample in time_midpoint:
    gaussian_pulse(time_sample - 0.5*time_step/math.sqrt(3), 0, pulse_sample)
    time_quadrature += [time_sample - 0.5*time_step/math.sqrt(3)]
    pulse_quadrature += [pulse_sample[0]]

    gaussian_pulse(time_sample + 0.5*time_step/math.sqrt(3), 0, pulse_sample)
    time_quadrature += [time_sample + 0.5*time_step/math.sqrt(3)]
    pulse_quadrature += [pulse_sample[0]]
time_quadrature = np.asarray(time_quadrature)
pulse_quadrature = np.asarray(pulse_quadrature)
plt.plot(time_quadrature, pulse_quadrature, "m.")

plt.xlabel("Time (standard deviations)")
plt.ylabel("Pulse strength (Hz)")
plt.legend(
    [
        "Pulse shape",
        "Integration steps",
        "Pulse sample points"
    ]
)
plt.title("{}\nSample points for integrating Gaussian pulse".format(time_now_string))
plt.savefig("gaussian_pulse_sample.png")
plt.savefig("gaussian_pulse_sample.pdf")
plt.show()

plt.figure()

time = np.arange(-5.0, 5.1, 2.0)
state_analytic = np.asarray([[math.cos(0.5*math.pi*cumulative_gaussian(t)), -1j*math.sin(0.5*math.pi*cumulative_gaussian(t))] for t in time], dtype = np.complex128)

simulator = spinsim.Simulator(gaussian_pulse, spinsim.SpinQuantumNumber.HALF)

result_compare = simulator.evaluate(0.0, -5.0, 7.0, 1e-6, 2.0, np.asarray([1, 0], np.complex128))

legend = []

colours = ["r", "g", "b", "c", "m", "y"]
error = []
time_steps = np.asarray([0.001, 0.005, 0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0])
number_of_steps = 10 / time_steps
plot_start_index = 4
for simulation_index, time_step in enumerate(time_steps):
    result_simulated = simulator.evaluate(0.0, -5.0, 7.0, time_step, 2.0, np.asarray([1, 0], np.complex128))

    error += [np.sum(np.abs(result_simulated.state - result_compare.state))/5]

    if simulation_index >= plot_start_index:
        plt.plot(time, result_simulated.spin[:, 0], colours[simulation_index - plot_start_index] + "--o")
        plt.plot(time, result_simulated.spin[:, 1], colours[simulation_index - plot_start_index] + "--x")
        plt.plot(time, result_simulated.spin[:, 2], colours[simulation_index - plot_start_index] + "--+")

        legend += [
            "{:d} x".format(int(number_of_steps[simulation_index])),
            "{:d} y".format(int(number_of_steps[simulation_index])),
            "{:d} z".format(int(number_of_steps[simulation_index]))
        ]

plt.legend(legend, loc = "lower left")
plt.xlabel("Time (standard deviations)")
plt.ylabel("Spin")
plt.title("{}\nGaussian pulse at various numbers of steps".format(time_now_string))
plt.savefig("gaussian_pulse.png")
plt.savefig("gaussian_pulse.pdf")
plt.show()

plt.figure()
plt.loglog(number_of_steps, error, "-rx")
plt.xlabel("Number of steps")
plt.ylabel("Error")
plt.title("{}\nError in integrating Gaussian pulse".format(time_now_string))
plt.ylim((1e-12, 1e0))
plt.savefig("gaussian_pulse_error.png")
plt.savefig("gaussian_pulse_error.pdf")
plt.show()


# shows how linear regression analysis can be applied to polynomial data
#
# notes for this course can be found at:
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

import numpy as np
import matplotlib.pyplot as plt

# load the data
X = []
Y = []
for line in open('data_poly.csv'):
    x, y = line.split(',')
    x = float(x)
    X.append([1, x, x*x]) # add the bias term x0 = 1
    Y.append(float(y))

# let's turn X and Y into numpy arrays since that will be useful later
X = np.array(X)
Y = np.array(Y)

# let's plot the data to see what it looks like
plt.scatter(X[:,1], Y)
plt.show()

# apply the equations we learned to calculate a and b
# numpy has a special method for solving Ax = b
# so we don't use x = inv(A)*b
# note: the * operator does element-by-element multiplication in numpy
#       np.dot() does what we expect for matrix multiplication
w = np.linalg.solve(np.dot(X.T, X), np.dot(X.T, Y))
Yhat = np.dot(X, w)

# let's plot everything together to make sure it worked
plt.scatter(X[:,1], Y)
plt.plot(sorted(X[:,1]), sorted(Yhat))
# note: shortcut since monotonically increasing
#       x-axis values have to be in order since the points
#       are joined from one element to the next
plt.show()

# determine how good the model is by computing the r-squared
d1 = Y - Yhat
d2 = Y - Y.mean()
r2 = 1 - d1.dot(d1) / d2.dot(d2)
print("the r-squared is:", r2)


#Relu activation function
import numpy as np

input = [0.3,0.4,0.6,0.3,.49, -.57]
output =[]

for i in input:
    if i > 0:
        output.append(i)
    else:
        output.append(0)

print(output)

'''Second Option'''

outputs = []
for i in input:
    outputs.append(max(0,i))
print("2nd Option of Calculation : ",outputs)

'''Third option'''

output_values=np.maximum(0,input)
print("with Numpy values", output_values)

# !---'''Activation SoftMax'''---!
outputs_value =[4.8, 1.21, 2.385]
E = 2.71
output_ce =[]
for i in outputs_value:
    output_ce.append(E**i)
print(output_ce)
exponeential_sum = sum(output_ce)

output_val =[]

for j in output_ce:
    output_val.append(j/ exponeential_sum)
print("Cross Entropy Values : ", output_val)

'''Lets try about Numpy values'''
outputs =([[1.8, 1.21, 3.385], [2, 1.21, 2.385],[3, 1.21, 2.385]])
exp_val = np.exp(outputs- np.max(outputs, axis=1, keepdims=True))
print("Hey, Um exp_val", exp_val)
final_output = exp_val/np.sum(exp_val, axis=1, keepdims=True)
print("Final output of SoftMax will be : ", final_output)


import numpy as np

def mean_square_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Calculate MSE loss

    Parameters
    ----------
    y_true: ndarray of shape (n_samples, )
        True response values
    y_pred: ndarray of shape (n_samples, )
        Predicted response values

    Returns
    -------
    MSE of given predictions
    """
    #return float(np.mean((y_true-y_pred)**2))
    return float(sum(pow(y_true-y_pred,2))/len(y_true))

def misclassification_error(y_true: np.ndarray, y_pred: np.ndarray, normalize: bool = True) -> float:
    """
    Calculate misclassification loss

    Parameters
    ----------
    y_true: ndarray of shape (n_samples, )
        True response values
    y_pred: ndarray of shape (n_samples, )
        Predicted response values
    normalize: bool, default = True
        Normalize by number of samples or not

    Returns
    -------
    Misclassification of given predictions
    """
    if y_true.size == 0 or y_pred.size == 0:
        return 0

    equal = np.zeros(y_true.shape[0])
    equal[y_true != y_pred] = 1
    counter = equal.sum(axis=0)
    if normalize:
        return counter/y_true.shape[0]
    return counter
    #y_true = np.argsort(y_pred)
    # equal = np.zeros(y_true.shape[0])
    # equal[y_pred == y_true] = 1
    # sumi = equal.sum(axis = 0)
    # if normalize:
    #     return sumi/(y_true.shape[0])
    # else:
    #     return sumi

def accuracy(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Calculate accuracy of given predictions

    Parameters
    ----------
    y_true: ndarray of shape (n_samples, )
        True response values
    y_pred: ndarray of shape (n_samples, )
        Predicted response values

    Returns
    -------
    Accuracy of given predictions
    """
    equal = np.zeros(y_true.shape[0])
    equal[y_pred == y_true] = 1
    return np.sum(equal)/(y_true.shape[0])

def cross_entropy(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """
    Calculate the cross entropy of given predictions

    Parameters
    ----------
    y_true: ndarray of shape (n_samples, )
        True response values
    y_pred: ndarray of shape (n_samples, )
        Predicted response values

    Returns
    -------
    Cross entropy of given predictions
    """
    raise NotImplementedError()


import numpy as np
class Solution:
    def knightDialer(self, N: int) -> int:
        mod = 10**9 + 7
        if N == 1: return 10
        M = np.matrix([[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
                       [0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
                       [0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
                       [1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [1, 1, 0, 0, 0, 0, 0, 1, 0, 0],
                       [0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
                       [0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
                       [0, 0, 1, 0, 1, 0, 0, 0, 0, 0]])
        res, N = 1, N - 1
        while N:
            if N % 2: res = res * M % mod
            M = M * M % mod
            N //= 2
        return int(np.sum(res)) % mod

import numpy as np
class Solution:
    def knightDialer(self, N: int) -> int:
        x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = x0 = 1
        for i in range(N - 1):
            x1, x2, x3, x4, x5, x6, x7, x8, x9, x0 = \
                x6 + x8, x7 + x9, x4 + x8, \
                x3 + x9 + x0, 0, x1 + x7 + x0, \
                x2 + x6, x1 + x3, x2 + x4, \
                x4 + x6
        return (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x0) % (10**9 + 7)

Dynamic Programming/935. Knight Dialer.py


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")

data=pd.read_csv("Salary_Data.csv")
#EDA-Explomatory data analytis
sns.lmplot(data=data,x="YearsExperience",y="Salary")
#sns.boxplot(data=data,x="YearsExperience")
sns.boxplot(data=data,x="Salary")

x=data.iloc[:,0].values.reshape(-1,1)
y=data.iloc[:,1].values.reshape(-1,1)

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)

from sklearn.linear_model import LinearRegression
lr=LinearRegression()
lr.fit(x_train,y_train)

y_pred=lr.predict(x_test)

plt.plot(x_test, y_pred,c="r")
plt.scatter(x_test, y_test)
plt.xlabel("YearsExperience")
plt.ylabel("Salary")
plt.show()

import math
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
mse=mean_squared_error(y_test,y_pred)
print("MSE:",mse)
rmse=math.sqrt(mse)
print("RMSE:",rmse)
r2=r2_score(y_test,y_pred)
print("R2 score:",r2)

#Manual method
#Using Formulas

def slope(x,y):
    x_mean=x.mean()
    y_mean=y.mean()

    x_mean_dif=[]
    y_mean_dif=[]

    for i in x:
        x_mean_dif.append(i-x_mean)
    for j in y:
        y_mean_dif.append(j-y_mean)

    x_mean_dif=np.array(x_mean_dif)
    y_mean_dif=np.array(y_mean_dif)

    x_y_mul=sum(x_mean_dif*y_mean_dif)

    x_square=sum(map(lambda x:x**2,x_mean_dif))

    m=x_y_mul/x_square

    return m

def constant(m,x_mean,y_mean):
    b=y_mean-(m*x_mean)
    return b

def predict(m,b,test):
    pred=[]
    for i in test:
        pred.append((m*i)+b)

    return np.array(pred)

m=slope(x_train,y_train)
b=constant(m,x_train.mean(),y_train.mean())
pred=predict(m,b,x_test)

print(m,b)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

data=pd.read_csv("Real estate.csv")
print(data.columns)
sns.pairplot(data)

print(data.isna().sum())

x=data.iloc[:,:-1].values
y=data.iloc[:,-1].values

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)

from sklearn.preprocessing import StandardScaler
sd=StandardScaler()
x_train=sd.fit_transform(x_train)
x_test=sd.transform(x_test)

from sklearn.linear_model import LinearRegression
lr=LinearRegression()
lr.fit(x_train,y_train)

y_pred=lr.predict(x_test)

import math
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
mse=mean_squared_error(y_test,y_pred)
print("MSE:",mse)
rmse=math.sqrt(mse)
print("RMSE:",rmse)
r2=r2_score(y_test,y_pred)
print("R2 score:",r2)


import numpy as np

def f(x):
    return x**6-x**4-x**3-1

x1=float(input("x1: "))
x2=float(input("x2: "))
d=float(input("Accuracy: "))
xm=float()
while abs(x2-x1)>=d and abs(f(xm))>=d:
    xm = x2 - (x2-x1) / (f(x2)-f(x1)) * f(x2)
    x1 = x2
    x2 = xm
    print("Root=","%.5f" % xm)
if f(xm)>d:
    print("Not convergent")
else:
    print("Final Solution=","%.5f" % xm)

Root Finding/Secant Method [Single Root].py


#Basic Gauss Elimination Methods

"""
Name - Suryabrata Das

Sem: V

College_Roll_NO: 703

Paper-code: CMSA DSE-IB

Registration No: A01-1112-117-003-2018

Examination roll_no: 2021151264

Subject: Numerical Methods (DSE-I)

"""
import numpy as np
def swap_row(mat,r1,r2):
    temp = mat[r1]
    mat[r1] = mat[r2]
    mat[r2] = temp

def normalize(mat,i):
    for j in range(i+1,len(mat)):
        fact = (mat[j][i]/mat[i][i])
        for k in range(0,len(mat)+1):
            mat[j][k] -= fact*mat[i][k]

def fwd_elimination(mat):
    n = len(mat)
    print("\n\n")
    for i in range(n-1):
        if mat[i][i] == 0:
            swap_row(mat,i,i+1)
            normalize(mat,i)
            m = np.array(mat)
            m = m[:,:n]
            if (np.linalg.det(m) == 0):
                print ("Singular Matrix!! No Unique solution Exists!!")
                return False

            return True

def print_mat(mat):
    for i in range(len(mat)):
        print(mat[i])
        print("\n")
def bwd_elimination(mat):
    n = len(mat)

    x = [0]*n
    for i in range(n-1,-1,-1):
        x[i] = mat[i][n]
        for j in range(i+1,n):
            x[i] -= mat[i][j]*x[j]
            x[i] = x[i]/mat[i][i]
            print("Solutions are: \n\n")
            print("x: ",x[0],"\n\n")
    print("y: ",x[1],"\n\n")
    print("z: ",x[2],"\n\n")

def main():
    n = 3
    mat = []
    mat = [[3,6,1,16],[2,4,3,13],[1,3,2,9]]
    print("Your input matrix: ")
    print_mat(mat)
    if(fwd_elimination(mat) == True):
        print("After Forward Elimination: ")
        print_mat(mat)
        bwd_elimination(mat)
if __name__ == '__main__':
    main()


"""
ndarray indexing
"""

import numpy as np

# given this
arr = np.array([[-1, 2, 0, 4],
                [4, -0.5, 6, 0],
                [2.6, 0, 7, 8],
                [3, -7, 4, 2.0]])

# grab first two rows of arr
a = None

# grab second and third row of arr
b = None

# grab first two rows of arr and first two columns
c = None

# grab elements at indices (0, 3), (1, 2), (2, 1), (3, 0)
d = None

# grab elements > 0
e = None


import math
import random
from typing import Optional, Tuple

import numpy as np
import pytest
from scipy import stats

from vallenae.features import (
    amplitude_to_db,
    counts,
    db_to_amplitude,
    energy,
    first_threshold_crossing,
    is_above_threshold,
    peak_amplitude,
    peak_amplitude_index,
    rise_time,
    signal_strength,
    rms,
)

LEN: int = 100
SAMPLERATES: Tuple[int] = (1, 10, int(1e6))

@pytest.fixture(name="random_array", scope="module")
def fixture_random_array():
    return np.random.rand(LEN)

def test_db_conversion():
    # 0 dB(AE) = 1 µV
    assert amplitude_to_db(1e-6) == 0
    assert db_to_amplitude(0) == 1e-6
    # 120 dB(AE) = 1 V?
    assert amplitude_to_db(1) == 120
    assert db_to_amplitude(120) == 1

def test_peak_amplitude(random_array):
    def naive(data: np.ndarray) -> float:
        return np.max(np.abs(data))

    assert peak_amplitude(random_array) == naive(random_array)

    # test negative amplitude
    arr = np.ones(5)
    arr[3] = -10
    assert peak_amplitude(arr) == naive(arr)

def test_peak_amplitude_index(random_array):
    def naive(data: np.ndarray) -> int:
        return np.argmax(np.abs(data))

    assert peak_amplitude_index(random_array) == naive(random_array)

def test_is_above_threshold():
    # def naive(data: np.ndarray, threshold: float) -> bool:
    #     return np.max(data) >= threshold

    arr = np.random.rand(LEN)
    assert is_above_threshold(arr, -1)
    assert is_above_threshold(arr, 0)
    arr[-1] = 1
    assert is_above_threshold(arr, 1)
    arr[0] = 2
    assert is_above_threshold(arr, 2)
    assert not is_above_threshold(arr, 2.01)

def test_first_threshold_crossing():
    def naive(data: np.ndarray, threshold: float) -> Optional[int]:
        above_threshold = np.abs(data) >= threshold
        index = np.argmax(
            above_threshold
        )  # = 0 if no crossing was found, might be the first sample though
        if index == 0 and not above_threshold[0]:
            return None
        return index

    arr = np.random.rand(LEN)
    thr = abs(random.random())
    assert first_threshold_crossing(arr, thr) == naive(arr, thr)

    arr[0] = 1
    thr = 1
    assert first_threshold_crossing(arr, thr) == 0

    arr[-1] = 2
    thr = 2
    assert first_threshold_crossing(arr, thr) == LEN - 1

@pytest.mark.parametrize("samplerate", SAMPLERATES)
def test_rise_time(samplerate: int):
    arr = np.random.rand(LEN)

    arr[0] = 2
    assert rise_time(arr, 2, samplerate) == 0

    arr[-1] = 3
    assert rise_time(arr, 0, samplerate) == (LEN - 1) / samplerate

@pytest.mark.parametrize("samplerate", SAMPLERATES)
def test_energy(random_array, samplerate: int):
    def naive(data: np.ndarray, samplerate: int) -> float:
        return np.sum(data ** 2) * 1e14 / samplerate

    assert energy(random_array, samplerate) == pytest.approx(naive(random_array, samplerate))

@pytest.mark.parametrize("samplerate", SAMPLERATES)
def test_signal_strength(random_array, samplerate: int):
    def naive(data: np.ndarray, samplerate: int) -> float:
        return np.sum(abs(data)) * 1e9 / samplerate

    assert signal_strength(random_array, samplerate) == pytest.approx(naive(random_array, samplerate))

def test_counts(random_array):
    def naive(data: np.ndarray, threshold: float) -> int:
        above_positive_threshold = (data >= threshold).astype(int)
        positive_crossings = np.count_nonzero(np.diff(above_positive_threshold) == 1)
        if above_positive_threshold[0]:
            positive_crossings += 1
        return positive_crossings

    arr = np.zeros(LEN)
    assert counts(arr, 0) == 1
    assert counts(arr, 1) == 0

    arr[::2] = 1
    assert counts(arr, 1) == LEN / 2

    for threshold in (-1, 0, 0.5, 1):
        assert counts(random_array, threshold) == naive(random_array, threshold)

def test_rms(random_array):
    def naive(data: np.ndarray):
        return math.sqrt(np.sum(data ** 2) / len(data))

    assert rms(random_array) == pytest.approx(naive(random_array))


import random
import math
import numpy as np

# Prim's Minimum Spanning Tree Algorithm
def primsAlgo(e,c,n):
	sel_edge = ()
	mincost = 0
	near = [0 for x in range(n)]
	t = [[0 for x in range(2)] for y in range(n-1)]
	print('Initially Minimum Spanning Tree: \n' ,np.matrix(t))
	values = np.array(c)
	index = np.argmin(values)
	minedge = [index//n, index%n]

	sel_edge = minedge
	mincost += c[sel_edge[0]][sel_edge[1]]

    #Select neighboring edge with minimum cost
	t[0][0],t[0][1] = sel_edge[0],sel_edge[1]
	for l in range(0,n):
		if(c[l][sel_edge[0]] < c[l][sel_edge[1]]):
			near[l] = sel_edge[0]
		else:
			near[l] = sel_edge[1]
	near[sel_edge[0]],near[sel_edge[1]] = -1,-1
	minm = math.inf
	for i in range(1,n-1):
		for j in range(n):
			if(near[j] != -1):
				if(c[j][near[j]] < minm):
					minm = c[j][near[j]]
					sel_edge[0] = j
					sel_edge[1] = near[j]
		minm = math.inf
		t[i][0],t[i][1] = sel_edge[0],sel_edge[1]
		mincost += c[sel_edge[0]][sel_edge[1]]
		near[sel_edge[0]] = -1
		for k in range(n):
			if(near[k] != -1 and (c[k][near[k]] > c[k][sel_edge[0]])):
				near[k] = sel_edge[0]
	print('Resultant MST: \n', np.matrix(t))
	return mincost

n = random.randint(5,7)
print('Number of vertices:', n)
max_int = math.inf
e = [[0 for x in range(n)] for y in range(n)]
c = [[max_int for x in range(n)] for y in range(n)]
for i in range(n):
	for j in range(n):
		if(i < j):
			e[i][j]=1
			c[i][j]=float(random.randint(10,50))
		else:
			e[i][j]=e[j][i]
			c[i][j]=c[j][i]
print('Edge Adjacency Matrix: \n', np.matrix(e))
print('Cost Adjacency Matrix: \n',np.matrix(c))
print ('Cost of MST: ', primsAlgo(e,c,n))


""" When you Fourier Transform an Image most of the fourier coefficients are quite small and
    can be truncated or thresholded away so we can zero out all of the small fourier coefficients
    retaining only the largest 1% or 2% of those FFT values and then

    When we inverse Fourier Transform that thresholded FFT signal we recover the original image
    with relatively low degradation

    So compression comes about by only saving these largest %1 or %2 fourier coefficients """

from matplotlib.image import imread
import numpy as np
import matplotlib.pyplot as plt
import os

from mpl_toolkits.mplot3d import axes3d

path = r'C:\Users\oguzhan.sari\Desktop\Oğuzhan\development\fourier_SB\image compression FFT'
plt.rcParams['figure.figsize'] = [5, 5]
plt.rcParams.update({'font.size': 18})

A = imread(os.path.join(path, 'headshot.jpg'))
B = np.mean(A, -1)         # converts RGB to grayscale

plt.figure()
plt.imshow(A)
plt.axis('off')
plt.show()

plt.imshow(B)               # grayscale --> average of the color channels RGB
# plt.imshow(256 - A)       #,cmap='gray_r')         # steve-black background
plt.axis('off')
plt.show()
""" We just want to work on an array for simplicity, not to work on three color channel """

Bt = np.fft.fft2(B)
Btsorted = np.sort(np.abs(Bt.reshape(-1)))          # sort by magnitude

# Zero out all small coefficients and inverse transform
for keep in (0.1, 0.05, 0.01, 0.002):
    thresh = Btsorted[int(np.floor((1-keep)*len(Btsorted)))]
    ind = np.abs(Bt) > thresh                                   # find small indices
    Btlow = Bt * ind                                         # Threshold small indices  # mask [1, .. 0] ones and zeros
    Alow = np.fft.ifft2(Btlow).real                             # compressed image
    plt.figure()
    plt.imshow(Alow, cmap='gray')
    # plt.imshow(256- Alow, cmap='gray')
    plt.axis('off')
    plt.title('Compressed image: keep = ' + str(keep*100) + '%')
    plt.show()

""" There is a lot of high frequency content in hair, fur and grass, thats when we need to keep to reconstruct
    that image and so high frequency stuff is harder to represent

Pixar - Toy Story;
    Characters had plastic skin without hair and fur no grass
    because it is much much easier to work in a compressed representation and to render those types of objects
    than to render hair, fur and grass

    And it was big step from Toy Story to Monsters Inc where they finally figured out how to render big hairy monster
    because that's a lot harder, a lot higher frequency content, a lot more detail you have to render that you can't
    just compress out """

""" The Larger the image the more compressible it is
    So if we have an ultra ultra high-res image, there's more redundant information that we can compress out
    We'll get a higher compression ratio the bigger the image

    If we have smaller image low-res, we need more of those fourier coefficients to reconstruct the image """

# Plot pixel intensity of the image as a surface

plt.rcParams['figure.figsize'] = [6, 6]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y = np.meshgrid(np.arange(1, np.shape(B)[1] + 1), np.arange(1, np.shape(B)[0] + 1))
ax.plot_surface(X[0::10, 0::10], Y[0::10, 0::10], 256-B[0::10, 0::10], cmap='viridis', edgecolor='none')
ax.set_title = 'Surface Plot'
ax.mouse_init()
#ax.view_init(200, 270)
ax.view_init(-270, -270)              #different perspective
plt.show()

""" this is the exact headshot.jpg but rendered as a surface plot
    So brighter pixels in my face are both brighter and taller - kind of an elevation map

    My face kind of mountain landscape of pixel intensity
    the whites of my eye were the brightest pixels, those are the tallest peaks

    What we are trying to do with fourier approximation is trying the approximate this mountain land scape
    of the pixel intensity as a sum of sin() and cosine() waves in X and in Y at different frequences
So we are going to add up all of those kind of wavy sheets in just the right proportion and
we're going to reconstruct this topography of pixel intensities to reconstruct my face"""


import sys
import math
import pandas as pd
import numpy as np
def calculate_initial_compass_bearing(pointA, pointB):
    if (type(pointA) != tuple) or (type(pointB) != tuple):
        raise TypeError("Only tuples are supported as arguments")
    lat1 = math.radians(pointA[0])
    lat2 = math.radians(pointB[0])
    diffLong = math.radians(pointB[1] - pointA[1])
    x = math.sin(diffLong) * math.cos(lat2)
    y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1) * math.cos(lat2) * math.cos(diffLong))
    initial_bearing = math.atan2(x, y)
    # Now we have the initial bearing but math.atan2 return values
    # from -180° to + 180° which is not what we want for a compass bearing
    # The solution is to normalize the initial bearing as shown below
    initial_bearing = math.degrees(initial_bearing)
    compass_bearing = (initial_bearing + 360) % 360
    return compass_bearing

def convert_to_distance_bearing(pointAlat, pointAlon, pointBlat, pointBlon):
	pointA=(pointAlat,pointAlon)
	pointB=(pointBlat,pointBlon)
	A=calculate_initial_compass_bearing(pointA, pointB)
	from math import sin, cos, sqrt, atan2, radians
	# approximate radius of earth in km
	R = 6373.0
	lat1 = radians(pointAlat)
	lon1 = radians(pointAlon)
	lat2 = radians(pointBlat)
	lon2 = radians(pointBlon)
	dlon = lon2 - lon1
	dlat = lat2 - lat1
	a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
	c = 2 * atan2(sqrt(a), sqrt(1 - a))
	distance = R * c
	return distance, A

def transform_dataset(datasetfile):
	dataset_coords = pd.read_csv(datasetfile, engine='python').values.astype('float32')
	dataset=[0,0]
	for i in range(len(dataset_coords)-1):
		j=dataset_coords[i]
		k=dataset_coords[i+1]
		dis, bear = convert_to_distance_bearing(j[0], j[1], k[0], k[1])
		results=[dis, bear]
		dataset=np.vstack((dataset,results))
	dataset=np.delete(dataset, 0, 0)
	return 	dataset


"""Functions related to modular arithmetic"""

from math import prod
from typing import List

from sympy.ntheory.factor_ import totient

# https://en.wikipedia.org/wiki/Modular_multiplicative_inverse#Using_Euler's_theorem
def modular_inverse(n:int,base:int) -> int:
    """Returns the modular inverse of n in base"""
    n **= totient(base) - 1
    n %= base
    return n

# https://crypto.stanford.edu/pbc/notes/numbertheory/crt.html
def chinese_remainder_theorem(a:List[int],m:List[int]) -> int:
    """Performs the chinese remainder theorem on recieved lists"""

    if len(a) != len(m):
        raise ValueError('Length of a must match length of m')

    M = prod(m)
    solution = 0
    for i,m_i in enumerate(a):
        b = M // m_i
        b_inv = modular_inverse(b,m_i)
        solution += a[i] * b * b_inv
    return solution % M


"""
Created on Sun Feb 20 21:31:53 2022

@author: udaytalwar
"""

#Please refer to "BlackScholesEqns.pdf" in this repository for more details about the equations!

import scipy.stats as stats
import math as m

def d(S, K, sigma, r, t):

    '''
    S = Current Price
    K = Strike Price
    sigma = Volatility
    r = annualized risk-free rate
    t = time to expiration

    returns d1, d2 for option price calculation using Black Scholes
    '''
    d1 = (m.log(S/K) + (r + (sigma**2/2))*t) * (1/(sigma*m.sqrt(t)))

    d2 = d1 - sigma*m.sqrt(t)

    return d1, d2

def option_price(S, K, sigma, r, t, flag, d1 = 0, d2 = 0):

    '''
    S = Current Price
    K = Strike Price
    sigma = Volatility
    r = annualized risk-free rate
    t = time to expiration
    flag = 'Call' or 'Put'

    returns option price according to Black Scholes
    '''

    if d1 == 0 and d2 == 0:

        d1, d2 = d(S, K, sigma, r, t)

        if flag == 'Call':

            price = stats.norm.cdf(d1)*S - stats.norm.cdf(d2)*K*m.exp(-r*t)

        elif flag == 'Put':

            price = stats.norm.cdf(-d2)*K*m.exp(-r*t) - stats.norm.cdf(-d1)*S

        return price

    else:

        if flag == 'Call':

            price = stats.norm.cdf(d1)*S - stats.norm.cdf(d2)*K*m.exp(-r*t)

        elif flag == 'Put':

            price = stats.norm.cdf(-d2)*K*m.exp(-r*t) - stats.norm.cdf(-d1)*S

        return price

def imp_vol(S, K, r, t, flag, option_CMP):

    '''
    S = Current Price
    K = Strike Price
    r = annualized risk-free rate
    t = time to expiration
    flag = 'Call' or 'Put'
    option_CMP = current market price of option

    returns implied volatility of option according to Black Scholes
    '''

    lb = -2  #lower bound of midpoint method

    ub = 5  #upper bound of midpoint method

    error = 1e-15  #error tolerance

        #midpoint method

    while (ub-lb) > error:

        if option_price(S, K, (ub+lb)/2, r, t, flag) - option_CMP > 0:
            ub = (lb+ub)/2

        else:
            lb = (lb+ub)/2

    return lb


import numpy as np
import matplotlib.pyplot as plt

class Perceptron:
    """
    Initialize Perceptron with inputs and output pairs. Set Weights to random values
    """
    def __init__(self,x,y):
        self.ntrain = x.shape[0]
        self.dim = x.shape[1]
        self.x = x
        self.y = y
        #self.w = np.random.random((self.dim,1))
        self.w = np.random.random((self.dim))
        #self.w = np.array([1.0,1.0,1.0])

    """
        Function that plots the boundary between datapoints in x. If provided a set of Weights,
        will plot all of them on the same graph with different points provided.
    """
    def plotBoundary(self, w = [], type = 'r--', labels = []):
        a = self.x[:,0]
        b = self.x[:,1]
        vals = {1:'+',-1:'<'}
        for i in xrange(len(self.x)):
            #print str(self.x[i][0])+ ' ' + str(self.x[i][1])
            plt.scatter(self.x[i][0],self.x[i][1],marker=vals[self.y[i]])
        x = np.linspace(0, 10, 10)
        if w == []:
            y = -1*(self.w[0]/self.w[1])*x - (self.w[2]/self.w[1])
            plt.plot(x,y ,type)
        else:
            type = ["r--","b--","g--","b-."]
            for i in xrange(0,len(w)):
                y = -1*(w[i][0]/w[i][1])*x - (w[i][2]/w[i][1])
                plt.plot(x,y ,type[i],label=labels[i])
            plt.legend(bbox_to_anchor=(0.5, 1), loc=2, borderaxespad=0.)
        #print y

        plt.show()

    """
        This method inverts the input points for points belonging to the second class.
    """
    def processInput(self):
        self.x[self.y < 0] = -1*self.x[self.y < 0]

    """
    Train a single perceptron with single sample training with the margin specified.
    """
    def train(self,a = 1, b = 0):
        self.processInput()
        e = 1
        k = 0
        while e > 0:
            e = 0
            k += 1
            for i in xrange(len(self.x)):
                val = np.sum(self.w*self.x[i])
                if val < b:
                    self.w[0] += a*self.x[i,0]
                    self.w[1] += a*self.x[i,1]
                    self.w[2] += a*self.x[i,2]
                    e += 1
        #print k
        self.processInput()
        #self.plotBoundary()
        #print self.w
        return self.w

    """
    Train a single perceptron with relaxation
    """
    def trainRelaxation(self,a = 1, b = 0 ):
        self.processInput()
        e = 1
        k = 0
        while e > 0:
            k += 1
            e = 0
            for i in xrange(len(self.x)):
                val = np.sum(self.w*self.x[i])
                #self.plotBoundary()
                #print val
                if val < b:
                    sumVal = np.sum(self.x[i]**2)
                    update = (b - val)/sumVal
                    #print update
                    self.w[0] += a*self.x[i,0]
                    self.w[1] += a*self.x[i,1]
                    self.w[2] += a*self.x[i,2]
                    e += 1
        #print k
        self.processInput()
        #self.plotBoundary()
        #print self.w
        return self.w

    """
    LMS procedure implemented for a given learning rate and margin
    """
    def trainLMS(self,a = 1,b = 0):
        self.processInput()
        e = 1
        k = 0
        eta = a
        while k < 1000:
            k += 1
            for i in xrange(len(self.x)):
                val = np.sum(self.w*self.x[i])
                if ((b - val) < 0.5):
                    update = 0
                else:
                    update = 1
                self.w += eta*update*self.x[i]
        self.processInput()
        print self.w
        self.plotBoundary()
        #print self.w
        return self.w

if __name__ == '__main__':
    x = np.array([[2, 7, 1], [8, 1, 1], [7, 5, 1], [6, 3, 1],[7, 8, 1],[5, 9, 1],[4, 5,1],[4, 2, 1],[-1, -1,1],[1, 3,1], [3, -2,1], [5, 4,1], [2, 4,1],[7, 1, 1]])
    y = np.array([1, 1, 1, 1, 1, 1, 1,-1, -1, -1, -1, -1, -1,-1])
    p = Perceptron(x,y)
    #p.plotBoundary()
    #w = [[ 0.5 ,  0.55, -4.5 ],[  3.2 ,   3.35, -27.9 ],[  2.685  ,   2.68375, -23.15   ],[  2. ,   1.9, -16.7]]
    #labels = ["Single-sample","Single-sample(margin)", "Relaxation(margin)","LMS"]
    #w.append(p.train(0.1,0))
    #w.append(p.train(0.1,1))
    #w.append(p.trainRelaxation(0.005,1))
    #w.append(p.trainLMS(0.1,1))
    p.trainLMS(0.1,1)
    #p.plotBoundary(w,labels=labels)