from numpy import *
import theano
import theano.tensor as T
from scipy.misc import logsumexp
from scipy.optimize import fmin_ncg

random.seed(1)

K = 5#nClasses
N = 10 #nSamples
Dout = 4 #hidden features
Din = 2 #input features

#single precision for now
theano.config.floatH = 'float32'

#setup toy example
X = random.randn(N, Din)
Win = random.randn(Din, Dout)
Wout = random.randn(Dout,K)
#hidden activations
Hlin = dot(X, Win)
H = tanh(Hlin)
Eta = dot(H,Wout)
lNorm = logsumexp(Eta, axis=1).reshape(N,1)
lP = Eta - lNorm
#take one sample from a multinomial distribution specified by a row of lP
_,y = apply_along_axis(lambda row: random.multinomial(1, exp(row)), axis=1, arr=lP).nonzero()
y = y.astype(int32)
Wout = Wout.astype(float32)
Win = Win.astype(float32)
H = H.astype(float32)
X = X.astype(float32)
Hlin = Hlin.astype(float32)

#setup theano
tX = T.matrix('X')
tWin = T.matrix('Win')
tWout = T.matrix('Wout')
ty = T.ivector('y')
tlambda = T.scalar('lambda')

#symbolic representation
tHlin = T.dot(tX, tWin)
tH = T.tanh(tHlin)
tEta = T.dot(tH, tWout)
tP = T.nnet.softmax(tEta)
terror = T.nnet.categorical_crossentropy(tP, ty).mean()
tgrad = T.grad(terror, [tWout, tWin])

#numeric functions
f = theano.function([tWout, tWin, tX, ty], terror)
g = theano.function([tWout, tWin, tX, ty], tgrad)

"""
Main Part of experiment:
    Can we manually invoke the chain rule to let theano compute gradients deeper down in the architecture?
    The goal is to compute the gradient wrt. parameters of the output layer by hand which is more convenient and then let theano figure out the rest, i.e. the parameters of the RNN backbone automatically
"""
#Compute reference gradient
gWout, gWin = g(Wout, Win, X,y)

#compute symbolic graident wrt. to hidden
tgH = T.grad(terror, tH)

#invoking chain rule manually with partial symbolic gradient with respect to hidden unit
tgWin2 = theano.gradient.Lop(tH, tWin, tgH)
gWin2 = tgWin2.eval({tX:X,ty:y,tWin:Win,tWout:Wout})

#can we do it also with an actual partial numeric result?
gH = tgH.eval({tX:X,ty:y,tWin:Win,tWout:Wout})
tgHdummy = T.matrix("tgHdummy")
tgWin3 = theano.gradient.Lop(tH, tWin, tgHdummy)
gWin3 = tgWin3.eval({tX:X,tWin:Win,tgHdummy:gH})


import numpy as np
from scipy.stats import norm, chi2

def Hartung(p, L=None, kappa=0.2, alpha=0.10):

    '''
     This function applies the modified inverse normal method for the combination of dependent p-values.

     Arguments:
         p:         vector of p-values.
         lambda:    vector of weights. It must be of the same length of p.
         kappa:     adjustment parameter. It is a positive value (0.2 is the default value),
                    then it is computed as in Hartung, p. 853.
         alpha:     level for the 1-alpha confidence interval for rho (0.10 is the default).

    Returns:
         Value: {"statistic": the Ht test statistic
        	     "parameter": the number of combined tests (p-values)
        	     "pvalue": the combined test p-value
        	     "conf_int": the confidence interval for the estimated correlation
        	     "estimate": the estimated correlation
        	     "null_value": the specified hypothesized value under the null
        	     "alternative": string describing the alternative hypothesis
        	     "method": string indicating the type of combination test (only Z-test is implemented)}

     Reference:
     Hartung, J. (1999): "A note on combining dependent tests of significance",
                         Biometrical Journal, 41(7), 849--855.
    '''

    if L == None:
        L = np.ones(len(p), dtype=float)

    t = norm.ppf(p)
    n = float(len(p))
    avt = np.sum(t)/n
    q = np.sum((t - avt)**2)/(n-1)                          # Hartung, eqn. (2.2)
    rhohat = 1 - q
    rhostar = max(-1/(n-1), rhohat)                           # Hartung, p. 851
    if kappa=="formula": kappa = (1 + 1/(n-1) - rhostar)/10  # Hartung, p. 853
    if kappa=="formula2": kappa = (1 + 1/(n-1) - rhostar)/5  # Hartung, p. 853

    # Hartung inverse normal corrected. See eqn. (2.4)

    Ht = np.sum(L*t)/np.sqrt(np.sum(L**2)+((np.sum(L))**2-np.sum(L**2))*(rhostar+kappa*np.sqrt(2/(n-1))*(1-rhostar)))
    lower = 1 - (n-1)/chi2.ppf(alpha/2, (n-1)) * q
    upper = 1 - (n-1)/chi2.ppf((1-alpha/2), (n-1)) * q          # Hartung, eqn. (2.3)

    output = dict(statistic=Ht,
                   parameter=n,
                   pvalue=norm.cdf(Ht),
                   conf_int=(lower, upper),
                   estimate=rhohat,
                   null_value="Ht=0",
                   alternative="less",
                   method="modified inverse normal combination")

    return output


""""
Function  : area_triangle_3d.py
Title     : Triangle area 3D
Written by: Alejandro Rios
Date      : November/2019
Last edit : August/2020
Language  : Python
Aeronautical Institute of Technology - Airbus Brazil

Description:
    - This module calculate the area triangle for applications in 3D surfaces area calculation

Future implementations:
    -

Inputs:
    - Triangle coordinates x, y, z
Outputs:
    - area
"""
########################################################################################
"""Importing Modules"""
########################################################################################
import numpy as np
########################################################################################
"""Function definition"""
########################################################################################

def area_triangle_3d(x,y,z):

    x1=x[0]
    x2=x[1]
    x3=x[2]
    #
    y1=y[0]
    y2=y[1]
    y3=y[2]
    #
    z1=z[0]
    z2=z[1]
    z3=z[2]
    #A=sqrt(s*(s-a)*(s-b)*(s-c))
    # Method  2
    # https://math.stackexchange.com/questions/128991/how-to-calculate-area-of-3d-triangle
    T1=(x2*y1 - x3*y1 - x1*y2 + x3*y2 + x1*y3 - x2*y3)**2
    T2=(x2*z1 - x3*z1 - x1*z2 + x3*z2 + x1*z3 - x2*z3)**2
    T3=(y2*z1 - y3*z1 - y1*z2 + y3*z2 + y1*z3 - y2*z3)**2
    area=(np.sqrt(T1+T2+T3)/2)
    return(area)


import numpy as np

def sph2cart(lon, lat, radius):
    lon_rad = np.deg2rad(lon)
    lat_rad = np.deg2rad(lat)
    x = radius * np.cos(lat_rad) * np.cos(lon_rad)
    y = radius * np.cos(lat_rad) * np.sin(lon_rad)
    z = radius * np.sin(lat_rad)
    return x, y, z


from __future__ import division
from scipy.stats import multivariate_normal
import scipy.stats as stats
import scipy.io as sio
import matplotlib.pyplot as plt
import numpy as np

def estimate_gaussian(X):
    mu = np.mean(X, axis=0)
    sigma2 = np.var(X, axis=0)
    return mu, sigma2

def select_threshold(y_val, p_val):
    best_epsilon, best_F1 = 0, 0

    step_size = (max(p_val) - min(p_val)) / 1000
    for epsilon in np.arange(p_val.min(), p_val.max(), step_size):
        predictions = (p_val < epsilon)[:, np.newaxis]
        tp = np.sum(predictions[y_val == 1] == 1)
        fp = np.sum(predictions[y_val == 0] == 1)
        fn = np.sum(predictions[y_val == 1] == 0)

        prec = tp / (tp + fp)
        rec = tp / (tp + fn)

        F1 = 2 * prec * rec / (prec + rec)

        if F1 > best_F1:
            best_epsilon = epsilon
            best_F1 = F1

    return best_epsilon, best_F1

def main():
    # 1
    file_path = 'ex8data1.mat'
    dataset = sio.loadmat(file_path)
    X = dataset["X"]
    Xval = dataset["Xval"]
    yval = dataset["yval"]

    # 2
    plt.scatter(X[:, 0], X[:, 1], marker="o", s=16)
    plt.xlim(0, 30)
    plt.ylim(0, 30)
    plt.xlabel("Latency (ms)")
    plt.ylabel("Throughput (mb/s)")
    plt.show()

    # 3
    mu, sigma2 = estimate_gaussian(X)

    # 4
    p = multivariate_normal(mu, np.diag(sigma2))
    print(mu, sigma2)

    # 5
    xs, ys = np.mgrid[0:30:0.1, 0:30:0.1]
    pos = np.empty(xs.shape + (2,))
    pos[:, :, 0] = xs
    pos[:, :, 1] = ys

    plt.figure(figsize=(8, 6))
    plt.plot(X.T[0], X.T[1], 'o', ms=4)
    plt.contour(xs, ys, p.pdf(pos), 10. ** np.arange(-21, -2, 3))
    plt.xlabel('Latency (ms)')
    plt.ylabel('Throughput (mb/s)')
    plt.xlim(0, 30)
    plt.ylim(0, 30)
    plt.show()

    # 6
    p_val = p.pdf(Xval)
    epsilon, F1 = select_threshold(yval, p_val)
    print("Best epsilon found using cross-validation:", epsilon)
    print("Best F1 on Cross Validation Set:", F1)

    # 7
    outliers = X[p.pdf(X) < epsilon]
    plt.figure(figsize=(8, 6))
    plt.plot(X.T[0], X.T[1], 'o', ms=4)
    plt.plot(outliers.T[0], outliers.T[1], 'o', ms=18, mfc='none', mec='r')
    plt.contour(xs, ys, p.pdf(pos), 10. ** np.arange(-21, -2, 3))
    plt.xlabel('Latency (ms)')
    plt.ylabel('Throughput (mb/s)')
    plt.xlim(0, 30)
    plt.ylim(0, 30)
    plt.show()

    # 8
    file_path = 'ex8data2.mat'
    dataset = sio.loadmat(file_path)
    X, Xval, yval = dataset['X'], dataset['Xval'], dataset['yval'][:, 0]

    # 9
    mu, sigma2 = estimate_gaussian(X)

    # 10
    p = multivariate_normal(mu, np.diag(sigma2))

    # 11
    p_val = p.pdf(Xval)
    epsilon, F1 = select_threshold(yval, p_val)
    print('Best epsilon found using cross-validation: %.2e' % epsilon)
    print('Best F1 on Cross Validation Set          : %f\n' % F1)
    print('  (you should see a value epsilon of about 1.38e-18)')
    print('  (you should see a Best F1 value of       0.615385)')

    # 12
    print('\n# Anomalies found: %d' % np.sum(p.pdf(X) < epsilon))

if __name__ == '__main__':
    main()


import numpy as np
from numpy.linalg import *
import time # for timing program
import sys # for printing to std out

dat = np.genfromtxt('../data/dat.txt',delimiter=' ')

y = dat[:,0]
X = dat[:,1:11]
K = X.shape[1]
n = y.shape[0]
Xt = X.transpose()
XXi = inv(np.dot(Xt,X))
b_mle = np.dot( np.dot(XXi, Xt), y )

a = 1
b = 1
B = 100000
s2 = 10

def ll(be, sig2):
    y_min_Xb = y - np.dot(X, be)
    return np.dot( y_min_Xb.transpose(), y_min_Xb) / (-2*sig2) - n/2 * np.log(sig2)

def lpb(be):
    return -np.dot( be.transpose(), np.dot(XXi, be) ) / (s2*2)

def lps(sig2):
    return (a-1) * np.log(sig2) - sig2 / b

def mvrnorm(M,S): # STOPPED HERE
    return M + np.dot( cholesky(S) , np.random.normal(0,1,M.shape[0]) )

#ll(b_mle, s2) # test
#lpb(b_mle) # test
#lps(s2) # test
mvrnorm(b_mle, np.eye(b_mle.shape[0]))

csb = 4 * XXi
css = 1
acc_b = 0
acc_s = 0

b_hat = np.zeros(shape=(B,K))
s2_hat = np.ones(B)

# Time this:
start = time.time()
for b in range(1,B):
    b_hat[b] = b_hat[b-1]
    s2_hat[b] = s2_hat[b-1]
    #
    # Update Beta:
    cand = mvrnorm(b_hat[b], csb)
    q = ( ll(cand, s2_hat[b]) + lpb(cand)
        - ll(b_hat[b], s2_hat[b]) - lpb(b_hat[b]) )
    if q > np.log( np.random.rand() ):
      b_hat[b] = cand
      acc_b += 1
    #
    # Update s2:
    cand = np.random.normal(s2_hat[b], css)
    if cand > 0:
      q = ( ll(b_hat[b], cand) + lps(cand)
          - ll(b_hat[b], s2_hat[b]) - lps(s2_hat[b]) )
      if q > np.log( np.random.rand() ):
        s2_hat[b] = cand
        acc_s += 1
    #
    if b%(B//10) == 0:
      print "\rProgress: %d%s" %(round(b*100/B),"%"),
      sys.stdout.flush()

end = time.time()

print "\n"
print "beta: ", b_hat[B*.9:B].mean(0)
print "s2:   ", s2_hat[B*.9:B].mean()

print "acc_b:", acc_b / B
print "acc_s:", acc_s / B

elapsed_time = end - start # 23 seconds
print "Time Elapsed: ", elapsed_time, "\n"


"""This module will use RANSAC to estimate the plane of a point cloud.

"""

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import time
import logging

from sklearn import linear_model

# Looks like this function needs to be compiled on first call or something similar.
# Below is a dummy calculation
# If I don't do this there is always a 23 ms overhead. Even with only 3 points!!
_, _, _, _ = np.linalg.lstsq(np.array([[0,0], [1, 0], [0, 1]]), np.array([0, 0, 0]), rcond=None)

def estimate_plane(pc:np.ndarray, stop_probability=0.95):
    """Will estimate a planes parameters using RANSAC

    Args:
        pc (np.ndarray): NX3 Point Cloud
        stop_probability (float, optional): sklearn.linear_model.See RANSACRegressor. Defaults to 0.95.

    Returns:
        tuple(ndarray, ndarray, RANSACRegressor): Returns a tuple of the centroid of the point cloud, unit normal, and the RANSAC object
    """
    x_train = pc[:, :2]
    y_train = pc[:, 2]
    ransac = linear_model.RANSACRegressor(linear_model.LinearRegression(), stop_probability=stop_probability)

    t0 = time.perf_counter()
    ransac.fit(x_train, y_train)
    t1 = time.perf_counter()
    ms  = (t1 - t0) * 1000

    logging.debug(f"Took {ms:.1f} ms to compute RANSAC for {pc.shape[0]:} points. # Trials: {ransac.n_trials_:}")

    z = lambda x,y: (ransac.estimator_.intercept_ + ransac.estimator_.coef_[0]*x + ransac.estimator_.coef_[1]*y)

    point_on_plane_0 = np.array([0, 0, z(0,0)])
    point_on_plane_1 = np.array([1, 0, z(1,0)])
    point_on_plane_2 = np.array([0, 1, z(0,1)])

    vec_a = point_on_plane_1 - point_on_plane_0
    vec_b = point_on_plane_2 - point_on_plane_0

    normal = np.cross(vec_a, vec_b)
    normal = normal / np.linalg.norm(normal, axis=0)
    centroid = np.mean(pc, axis=0)

    logging.debug("Normal: %r; Centroid: %r", normal, centroid)
    return normal, centroid, ransac

def plot_pc(pc, ax):
    normal, centroid, ransac = estimate_plane(pc)
    ax.plot3D(pc[:,0], pc[:,1], pc[:,2], 'or')
    ax.quiver(centroid[0], centroid[1], centroid[2], normal[0], normal[1], normal[2], length=2.0)

def test_wheelchair():
    pc_1 = np.load("scratch/plane_0_0.npy")
    pc_2 = np.load("scratch/plane_0_1.npy")
    pc_3 = np.load("scratch/plane_0_2.npy")

    fig = plt.figure()
    ax  = fig.add_subplot(111, projection='3d')

    plot_pc(pc_1, ax)
    plot_pc(pc_2, ax)
    plot_pc(pc_3, ax)

    ax.set_xlabel("X")
    ax.set_ylabel("Y")
    ax.set_xlim([-2, 2])
    ax.set_ylim([-2, 2])
    ax.set_zlim([-2, 2])
    ax.view_init(-7,-68)
    plt.show()

def main():
    np.random.seed(1)
    slope_x = 0.5
    slope_y = 0.5
    intercept= 1.0
    noise = 0.5

    NUM_POINTS = 10

    X, Y = np.mgrid[0:NUM_POINTS, 0:NUM_POINTS]
    PC = np.column_stack((X.ravel(), Y.ravel()))

    Z = []
    for i in range(PC.shape[0]):
        Z.append(slope_x * PC[i,0] + slope_y * PC[i, 1] + intercept)
    Z = np.array(Z) + np.random.randn(PC.shape[0]) * noise

    PC = np.column_stack((PC[:,0], PC[:, 1], Z))

    normal, centroid, ransac = estimate_plane(PC)

    # # the plane equation
    z = lambda x,y: (ransac.estimator_.intercept_ + ransac.estimator_.coef_[0]*x + ransac.estimator_.coef_[1]*y)
    x,y = np.mgrid[0:NUM_POINTS,0:NUM_POINTS]

    fig = plt.figure()
    ax  = fig.add_subplot(111, projection='3d')
    ax.plot3D(PC[:,0], PC[:,1], PC[:,2], 'or')

    ax.plot_surface(x, y, z(x,y))
    ax.quiver(centroid[0], centroid[1], centroid[2], normal[0], normal[1], normal[2], length=2.0)

    ax.set_xlabel("X")
    ax.set_ylabel("Y")
    ax.set_xlim([0, NUM_POINTS])
    ax.set_ylim([0, NUM_POINTS])
    ax.set_zlim([0, NUM_POINTS])
    ax.view_init(-7,-68)
    plt.show()

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    # test_wheelchair()
    main()


import argparse
import json
import numpy as np

def build_arg_parser():
    parser = argparse.ArgumentParser(description='Compute similarity score')
    parser.add_argument('--user1', dest='user1', required=True,
            help='First user')
    parser.add_argument('--user2', dest='user2', required=True,
            help='Second user')
    parser.add_argument("--score-type", dest="score_type", required=True,
            choices=['Euclidean', 'Pearson'], help='Similarity metric to be used')
    return parser

# Compute the Euclidean distance score between user1 and user2
def euclidean_score(dataset, user1, user2):
    if user1 not in dataset:
        raise TypeError('Cannot find ' + user1 + ' in the dataset')

    if user2 not in dataset:
        raise TypeError('Cannot find ' + user2 + ' in the dataset')

    # Movies rated by both user1 and user2
    common_movies = {}

    for item in dataset[user1]:
        if item in dataset[user2]:
            common_movies[item] = 1

    # If there are no common movies between the users,
    # then the score is 0
    if len(common_movies) == 0:
        return 0

    squared_diff = []

    for item in dataset[user1]:
        if item in dataset[user2]:
            squared_diff.append(np.square(dataset[user1][item] - dataset[user2][item]))

    return 1 / (1 + np.sqrt(np.sum(squared_diff)))

# Compute the Pearson correlation score between user1 and user2
def pearson_score(dataset, user1, user2):
    if user1 not in dataset:
        raise TypeError('Cannot find ' + user1 + ' in the dataset')

    if user2 not in dataset:
        raise TypeError('Cannot find ' + user2 + ' in the dataset')

    # Movies rated by both user1 and user2
    common_movies = {}

    for item in dataset[user1]:
        if item in dataset[user2]:
            common_movies[item] = 1

    num_ratings = len(common_movies)

    # If there are no common movies between user1 and user2, then the score is 0
    if num_ratings == 0:
        return 0

    # Calculate the sum of ratings of all the common movies
    user1_sum = np.sum([dataset[user1][item] for item in common_movies])
    user2_sum = np.sum([dataset[user2][item] for item in common_movies])

    # Calculate the sum of squares of ratings of all the common movies
    user1_squared_sum = np.sum([np.square(dataset[user1][item]) for item in common_movies])
    user2_squared_sum = np.sum([np.square(dataset[user2][item]) for item in common_movies])

    # Calculate the sum of products of the ratings of the common movies
    sum_of_products = np.sum([dataset[user1][item] * dataset[user2][item] for item in common_movies])

    # Calculate the Pearson correlation score
    Sxy = sum_of_products - (user1_sum * user2_sum / num_ratings)
    Sxx = user1_squared_sum - np.square(user1_sum) / num_ratings
    Syy = user2_squared_sum - np.square(user2_sum) / num_ratings

    if Sxx * Syy == 0:
        return 0

    return Sxy / np.sqrt(Sxx * Syy)

if __name__=='__main__':
    args = build_arg_parser().parse_args()
    user1 = args.user1
    user2 = args.user2
    score_type = args.score_type

    ratings_file = 'ratings.json'

    with open(ratings_file, 'r') as f:
        data = json.loads(f.read())

    if score_type == 'Euclidean':
        print("\nEuclidean score:")
        print(euclidean_score(data, user1, user2))
    else:
        print("\nPearson score:")
        print(pearson_score(data, user1, user2))


import numpy as np

def create_search_space(size, dim):
    _dict_ = {}
    for i in range(dim):
        _dict_[str(i)] = size

    return _dict_

def sphere_function(para, X, y):
    loss = []
    for key in para.keys():
        if key == "iteration":
            continue
        loss.append(para[key] * para[key])

    return -np.array(loss).sum()

def rastrigin_function(para, X, y):
    loss = []
    for key in para.keys():
        if key == "iteration":
            continue

        loss_1d = 1 + para[key] * para[key] - np.cos(2 * np.pi * para[key])
        loss.append(loss_1d)

    return -(np.array(loss).sum())

def ackley_function(para, X, y):
    x, y = para["x"], para["y"]

    loss = -20 * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y))) - np.exp(
        0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)) + np.exp(1) + 20
    )

    return -loss

def rosenbrock_function(para, X, y):
    x = np.array([para["x1"], para["x2"], para["x3"], para["x4"]])
    y = np.array([para["x0"], para["x1"], para["x2"], para["x3"]])

    loss = sum(100.0 * (x - y ** 2.0) ** 2.0 + (1 - y) ** 2.0)

    return -loss

def himmelblau_function(para, X, y):
    loss = (para["x"] ** 2 + para["y"] - 11) ** 2 + (
        para["x"] + para["y"] ** 2 - 7
    ) ** 2

    return -loss

dim_size = np.arange(-10, 10, 0.03)

sphere_function_search_config_3 = {sphere_function: create_search_space(dim_size, 3)}

rastrigin_function_search_config_3 = {
    rastrigin_function: create_search_space(dim_size, 3)
}

sphere_function_search_config_5 = {sphere_function: create_search_space(dim_size, 5)}

rastrigin_function_search_config_5 = {
    rastrigin_function: create_search_space(dim_size, 5)
}

sphere_function_search_config_7 = {sphere_function: create_search_space(dim_size, 7)}

rastrigin_function_search_config_7 = {
    rastrigin_function: create_search_space(dim_size, 7)
}

sphere_function_search_config_9 = {sphere_function: create_search_space(dim_size, 9)}

rastrigin_function_search_config_9 = {
    rastrigin_function: create_search_space(dim_size, 9)
}

ackley_function_search_config = {ackley_function: {"x": dim_size, "y": dim_size}}

rosenbrock_function_search_config = {
    rosenbrock_function: {
        "x0": dim_size,
        "x1": dim_size,
        "x2": dim_size,
        "x3": dim_size,
        "x4": dim_size,
    }
}

himmelblau_function_search_config = {
    himmelblau_function: {"x": dim_size, "y": dim_size}
}

test_func_search_configs = [
    sphere_function_search_config_3,
    rastrigin_function_search_config_3,
    sphere_function_search_config_5,
    rastrigin_function_search_config_5,
    sphere_function_search_config_7,
    rastrigin_function_search_config_7,
    sphere_function_search_config_9,
    rastrigin_function_search_config_9,
    ackley_function_search_config,
    rosenbrock_function_search_config,
    himmelblau_function_search_config,
]


import numpy as np
#Gauss Seidel iteration method
#this program is Write By Endurance ogun and is part of the Sfip program
#CopyRight:: can be use for any of your project but one should not delete this comment or claim ownship of the program.

def G_S(A,B):
    t=float(input("the toll::"))
    L = np.tril(A)
    U = A - L
    L_inv = np.linalg.inv(L)
    x = np.zeros_like(B)
    N = 1
    M = 0
    j=0
    while True:
        for i in range(N):
            j=j+1
            print("Iteration::" + str(j))
            Ux = np.dot(U, x)
            x_new = np.dot(L_inv, B - Ux)
            M = np.allclose(x, x_new, t)
           # if np.allclose(x, x_new, t):
            #    break
            x = x_new
            print(x)

        if M:
            break
        N = N + 1

def get_matrixA(m_len,variable_type=float):
    A=[]
    for i in range(1,m_len+1):
        for j in range(1,m_len+1):
            m_col_row= float(input("enter value for \n a"+str(i)+str(j)+"::"))
            A.append(m_col_row)

    A=np.array(A).reshape(m_len,m_len)

    print(A)
    return A

def get_matrixB(m_len, variable_type=float):
    B = []
    for h in range(1, m_len + 1):
        m_col = float((input("enter value for  \n b" + str(h) + "::")))
        B.append(m_col)

    B = np.array(B).reshape(m_len, 1)
    print(B)
    return B

def get_solution():
    N=int(input("dimension of Matrix:"))
    A=get_matrixA(N)
    B=get_matrixB(N)
    G_S(A,B)

get_solution()


import numpy as np

class LogisticRegression(object):
    '''
    Logistic regression python code.
    '''
    def __init__(self,trainingData, trainingCategory, cycleMax, stepSize):
        '''
        :param trainingData: input x matrix(N*M) in list.
        :param trainingCategory: input y vector (M*1) in list.
        :param cycleMax: the num of LR iteration times.
        :param stepSize: iteraion step length <<1.
        '''
        self.trainingData = trainingData
        self.trainingCategory = trainingCategory
        self.cycleMax = cycleMax
        self.stepSize = stepSize

    def buildMatrix(self):
        catVec = np.array(self.trainingCategory).reshape(len(self.trainingCategory),1)
        trainMat = self.trainingData.copy()
        for i in range(len(trainMat)):
            trainMat[i].append(1)

        return np.array(trainMat), catVec

    def Sigmoid(self, xMat, thetaVec):
        res = np.zeros(xMat.shape[0], dtype=np.float128)
        for i in range(xMat.shape[0]):
            res[i] = np.dot(xMat[i,:],thetaVec)

        res_0 = 1/(1+np.exp(-res))
        return res_0

    def iteration(self, trainingMat, catVec):
        xMat = trainingMat.copy()
        thetaVec = np.ones(len(self.trainingData[0]))
        for c in range(self.cycleMax):
            print(np.round(100*c/self.cycleMax,2),'%',end='\r')
            E = self.Sigmoid(xMat, thetaVec)
            E = E.reshape(len(self.trainingCategory),1)
            delta = self.stepSize/xMat.shape[0] * (E-catVec)
            delta = np.dot(np.transpose(delta),xMat)
            # print('delta:',delta.shape)
            thetaVec -= delta.reshape(len(xMat[0]))
        return thetaVec

    def run(self):
        trainMat, catVec = self.buildMatrix()
        theta_solu = self.iteration(trainMat, catVec)
        return theta_solu

    def plotResult(self, theta_solu):
        return

# if __name__ == "__main__":


import numpy as np

def calculate_power(v, i):
    p = 1.5 * (v.alpha * i.alpha + v.beta * i.beta)
    q = 1.5 * (v.beta * i.alpha - v.alpha * i.beta)
    return p, q

def calculate_current(v, p, q):
    i_alpha = 2 * (p * v.alpha + q * v.beta) / (v.alpha ** 2 + v.beta ** 2) / 3
    i_beta = 2 * (p * v.beta - q * v.alpha) / (v.alpha ** 2 + v.beta ** 2) / 3
    return i_alpha, i_beta

def angle_diff(theta1, theta2, deg=False):
    delta = theta1 - theta2
    if deg:
        try:
            delta = [-360. + d if d > 180. else d for d in delta]
            delta = [360. + d if d <= -180. else d for d in delta]
        except TypeError:
            if delta > 180:
                delta = -360. + delta
            if delta <= -180:
                delta = 360. + delta
    else:
        try:
            delta = [-2*np.pi + d if d > np.pi else d for d in delta]
            delta = [2*np.pi + d if d <= np.pi else d for d in delta]
        except TypeError:
            if delta > np.pi:
                delta = -(2 * np.pi) + delta
            if delta <= -np.pi:
                delta = (2 * np.pi) + delta
    return delta

def power_flow_2_bus(v1, theta1, v2, theta2, g, b):
    """ Calculate the power flow from bus 1 to bus 2 across the line with admittance g+jb
        from the given voltages and angles.
    """
    p = 0
    q = 0

    # Build admittance matrix values
    g12, b12 = -g, -b
    g11, b11 = g, b

    p += v1 * v2 * (g12 * np.cos(theta1 - theta2) + b12 * np.sin(theta1 - theta2))
    q += v1 * v2 * (g12 * np.sin(theta1 - theta2) - b12 * np.cos(theta1 - theta2))

    p += v1 * v1 *   g11
    q += v1 * v1 * - b11
    return p, q

if __name__ == "__main__":
    from numpy import testing
    # Test power flow equations with purely reactive line
    p1, q1 = power_flow_2_bus(1, 0, 0.9458, -0.0529, 0, -10)
    p2, q2 = power_flow_2_bus(0.9458, -0.0529, 1, 0, 0, -10)
    testing.assert_allclose([p1, q1], [0.5, 0.55], atol=1e-2)
    testing.assert_allclose([p2, q2], [-0.5, -0.5], atol=1e-2)
    print("p1, q1: ", p1, q1)
    print("p2, q2: ", p2, q2)


import numpy as np
import matplotlib.pyplot as plt
import gd
from builtins import range

if __name__ == "__main__":

    def f(xx):
        x = xx[0]
        y = xx[1]
        return 5 * x ** 2 - 6 * x * y + 3 * y ** 2 + 6 * x - 6 * y

    def df(xx):
        x = xx[0]
        y = xx[1]
        return np.array([10 * x - 6 * y + 6, -6 * x + 6 * y - 6])

    xmin, xmax, ymin, ymax = -3, 3, -3, 3
    algos = []
    initial = np.array([1, 1])
    alphas = [0.1, 0.2]
    for alpha in alphas:
        algo = gd.GradiantDecent(f, df, alpha)
        algo.solve(initial)
        algos.append(algo)

    xs = np.linspace(-2, 2, 300)
    ys = np.linspace(-2, 2, 300)
    xmesh, ymesh = np.meshgrid(xs, ys)
    xx = np.r_[xmesh.reshape(1, -1), ymesh.reshape(1, -1)]
    flg, ax = plt.subplots(1, 2)
    levels = [-3, -2.9, -2.8, -2.6, -2.4, -2.2, -2, -1, 0, 1, 2, 3, 4]
    for i in range(2):
        ax[i].set_xlim((xmin, xmax))
        ax[i].set_ylim((ymin, ymax))
        ax[i].set_title("alpha={}".format(alphas[i]))
        ax[i].scatter(initial[0], initial[1], color="k", marker="o")
        ax[i].plot(algos[i].path_[:, 0], algos[i].path_[:, 1], color="k", linewidth=1.5)
        ax[i].contour(xs, ys, f(xx).reshape(xmesh.shape), levels=levels, colors="k", linestyles="dotted")
    plt.show()


"""
This module contains helper functions for transforming variables
"""

import os
import sys
import inspect
import csv
import json
from math import cos, sin, acos, asin, atan, pi, sqrt
import matplotlib.pyplot as plt
import numpy as np
from pprint import pprint

def metres_to_inches(measure_metres, measure_is_list=False):
    """converts measurements from metres to inches"""
    if measure_is_list:
        measure_inches = []
        for measurement in measure_metres:
            measure_inches.append(measurement / 0.0254)
    else:
        measure_inches = measure_metres / 0.0254
    return measure_inches

def inches_to_metres(measure_inches, measure_is_list=False):
    """converts measurements from inches to metres"""
    if measure_is_list:
        measure_metres = []
        for measurement in measure_inches:
            measure_metres.append(measurement * 0.0254)
    else:
        measure_metres = measure_inches * 0.0254
    return measure_metres

def get_quat_angles(quat):
    """gets the euler angles from the quaternions"""
    q0 = quat[0]
    q1 = quat[1]
    q2 = quat[2]
    q3 = quat[3]
    phi = atan(
        ((2*((q0*q1) + (q2*q3))) /
         (1 - 2*(q1**2 + q2**2)))
    )
    theta = asin(
        2*((q0*q2) - (q3*q1))
    )
    psi = atan(
        ((2*((q0*q3) + (q1*q2))) /
         (1 - 2*(q2**2 + q3**2)))
    )
    return phi, theta, psi

def get_dcm_angle(dcm):
    """gets the theta from the given matrix"""
    theta = 0
    cos_theta = dcm[0, 0]
    sin_theta = dcm[1, 0]
    if (cos_theta > 0) and (sin_theta > 0):
        theta = acos(cos_theta)
    elif (cos_theta < 0) and (sin_theta > 0):
        theta = acos(cos_theta)
    elif (cos_theta > 0) and (sin_theta < 0):
        theta = asin(sin_theta)
    else:
        theta = asin(sin_theta)
    return theta

def rotation_2d(theta_value):
    """gives the rotational matrix for a given angle"""
    cos_theta = np.cos(theta_value)
    sin_theta = np.sin(theta_value)
    rot_mat = np.matrix([[cos_theta, -sin_theta], [sin_theta, cos_theta]])
    return rot_mat

def odom_offset(x_values, y_values, initial_positions):
    """offsets the odometry values based on initial positions"""
    total_values = len(x_values)
    count = 0
    new_x = [None] * total_values
    new_y = [None] * total_values

    while count < total_values:
        new_x[count] = x_values[count] + initial_positions[0]
        new_y[count] = y_values[count] + initial_positions[1]
        count += 1

    return new_x, new_y

def polar_to_cartesian(polar_values, two_dim=True):
    """Converts 2d polar coordinates to 2d cartesian coordinates"""
    cartesian_values = polar_values
    if two_dim:
        cartesian_values[0] = polar_values[0] * cos(polar_values[1])
        cartesian_values[1] = polar_values[0] * sin(polar_values[1])
    return cartesian_values

def cartesian_to_polar(cartesian_values, two_dim=True):
    """Converts 2d cartesian coordinates to 2d polar coordinates"""
    polar_values = cartesian_values
    if two_dim:
        polar_values[0] = sqrt(cartesian_values[0]**2 + cartesian_values[1]**2)
        polar_values[1] = atan(cartesian_values[1]/cartesian_values[0])
    return polar_values


from scipy.integrate import odeint

import matplotlib.pyplot as plt
import numpy as np

def suscetibles_derivative(compartments, beta, gamma):
    S, I, R = compartments
    return -beta * S * I

def infected_derivative(compartments, beta, gamma):
    S, I, R = compartments
    return beta * S * I - gamma * I

def recovered_derivative(compartments, beta, gamma):
    S, I, R = compartments
    return gamma * I

def compartments_derivative(compartments, beta, gamma):
    dSdt = suscetibles_derivative(compartments, beta, gamma)
    dIdt = infected_derivative(compartments, beta, gamma)
    dRdt = recovered_derivative(compartments, beta, gamma)
    return dSdt, dIdt, dRdt

def simulate_sir_epidemics(infected_people_per_day, infection_duration, days_of_simulation, initial_state):
    beta = infected_people_per_day
    gamma = 1.0 / infection_duration

    t = np.linspace(0, days_of_simulation - 1, days_of_simulation)

    # solve ODEs
    derivative = lambda compartments, t, beta, gamma: compartments_derivative(compartments, beta, gamma)

    ret = odeint(derivative, initial_state, t, args=(beta, gamma))
    S, I, R = ret.T # arrays with compartment variation through days

    return t, S, I, R

def plot_sir(t, S, I, R, time_label='days'):
  f, ax = plt.subplots(1,1,figsize=(10,4))
  ax.plot(t, S, 'b', alpha=0.7, linewidth=2, label='Susceptible')
  ax.plot(t, I, 'y', alpha=0.7, linewidth=2, label='Infected')
  ax.plot(t, R, 'g', alpha=0.7, linewidth=2, label='Recovered')

  ax.set_xlabel(f"Time ({time_label})", labelpad=10)
  ax.set_ylabel('Population (%)', labelpad=10)

  ax.yaxis.set_tick_params(length=0)
  ax.xaxis.set_tick_params(length=0)

  ax.grid(b=True, which='major', c='w', lw=2, ls='-')

  legend = ax.legend()
  legend.get_frame().set_alpha(0.5)

  for spine in ('top', 'right', 'bottom', 'left'):
      ax.spines[spine].set_visible(False)

  plt.show()


# Common imports
import numpy as np
import pandas as pd

def FrankeFunction(x,y):
	term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
	term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
	term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
	term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
	return term1 + term2 + term3 + term4

def create_X(x, y, n ):
	if len(x.shape) > 1:
		x = np.ravel(x)
		y = np.ravel(y)

	N = len(x)
	l = int((n+1)*(n+2)/2)		# Number of elements in beta
	X = np.ones((N,l))

	for i in range(1,n+1):
		q = int((i)*(i+1)/2)
		for k in range(i+1):
			X[:,q+k] = (x**(i-k))*(y**k)

	return X

# Making meshgrid of datapoints and compute Franke's function
n = 4
N = 1000
x = np.sort(np.random.uniform(0, 1, N))
y = np.sort(np.random.uniform(0, 1, N))
z = FrankeFunction(x, y)
X = create_X(x, y, n=n)

Xpd = pd.DataFrame(X)
Xpd = Xpd - Xpd.mean()
correlation_matrix = Xpd.cov()
print(correlation_matrix)


import tensorflow as tf
import tensorflow_probability as tfp
import keras.backend as K
import numpy as np

def gaussian_layer(layer):

    dim_layer = len(layer.get_shape())
    param1, param2 = tf.unstack(layer, num=dim_layer, axis=-1)
    param1 = tf.expand_dims(param1, -1)
    param2 = tf.expand_dims(param2, -1)

    #apply softplus to bound between -1 and 1
    mu = tf.keras.activations.linear(param1)
    #mean =  tf.keras.activations.softsign(a)
    var = tf.keras.activations.elu(param2) + 1
    #b = tf.nn.elu(a) + 1

    out_tensor = tf.concat((mu, var), axis = dim_layer-1)

    return out_tensor

def gaussian_layer_2d(layer):

    dim_layer = len(layer.get_shape())
    param1, param2 = tf.unstack(layer, num=dim_layer, axis=-1)
    param1 = tf.expand_dims(param1, -1)
    param2 = tf.expand_dims(param2, -1)

    mu = tf.keras.activations.linear(param1)
    #mean =  tf.keras.activations.softsign(a)
    var = tf.keras.activations.elu(param2) + 1

    out_tensor = tf.concat((mu, var), axis = dim_layer-1)

    return out_tensor

def gaussian_loss(y_true, y_pred):

  par1, par2 = tf.unstack(y_pred, num=2, axis=-1)
  mu = tf.expand_dims(par1, -1)
  var = tf.expand_dims(par2, -1)
  dist = tfp.distributions.Normal(loc=mu, scale=var)
  #dist = tfp.distributions.Normal(loc=mu, scale=sigma)

  return tf.reduce_mean(-dist.log_prob(y_true))

def gaussian_nll(ytrue, ypreds):
    """Keras implmementation of multivariate Gaussian negative loglikelihood loss function.
    This implementation implies diagonal covariance matrix.

    Parameters
    ----------
    ytrue: tf.tensor of shape [n_samples, n_dims]
        ground truth values
    ypreds: tf.tensor of shape [n_samples, n_dims*2]
        predicted mu and logsigma values (e.g. by your neural network)

    Returns
    -------
    neg_log_likelihood: float
        negative loglikelihood averaged over samples

    This loss can then be used as a target loss for any keras model, e.g.:
        model.compile(loss=gaussian_nll, optimizer='Adam')

    """

    #n_dims = int(int(ypreds.shape[1])/2)

    #mu = ypreds[:, 0:n_dims]
    #logsigma = ypreds[:, n_dims:]
    dims = ypreds.shape[1]
    mu = ypreds[:,0:1]
    logsigma = ypreds[:, 1:]

    mse = -0.5*K.sum(K.square((ytrue-mu)/K.exp(logsigma)),axis=1)
    sigma_trace = -K.sum(logsigma, axis=1)
    log2pi = -0.5*dims*np.log(2*np.pi)

    log_likelihood = mse+sigma_trace+log2pi

    return K.mean(-log_likelihood)


import numpy as np
import math

def rosenbrock(x):
    a = x[0:x.size - 1] ** 2 - x[1:x.size]
    b = x - 1
    return -(100 * np.inner(a, a) + np.inner(b, b))

def schwefel(x):
    fit = 0
    for i in range(x.size):
        fit += (x[0:i+1].sum()) ** 2
    return -fit

def ackley(x):
    return -(-20 * np.exp(-0.2 * np.sqrt(np.inner(x, x) / x.size)) - np.exp(
        np.cos(2 * np.pi * x).sum() / x.size) + 20 + np.e)

alpha = [1.00, 1.20, 3.00, 3.20]
A = np.array([[10.00, 3.00, 17.00, 3.50, 1.70, 8.00],
              [0.05, 10.00, 17.00, 0.10, 8.00, 14.00],
              [3.00, 3.50, 1.70, 10.00, 17.00, 8.00],
              [17.00, 8.00, 0.05, 10.00, 0.10, 14.00]])
P = 0.0001 * np.array([[1312, 1696, 5569, 124, 8283, 5886],
                       [2329, 4135, 8307, 3736, 1004, 9991],
                       [2348, 1451, 3522, 2883, 3047, 6650],
                       [4047, 8828, 8732, 5743, 1091, 381]])
def hartmann(x):
    """6d Hartmann test function
        input bounds:  0 <= xi <= 1, i = 1..6
        global optimum: (0.20169, 0.150011, 0.476874, 0.275332, 0.311652, 0.6573),
        min function value = -3.32237
    """
    external_sum = 0
    for i in range(4):
        internal_sum = 0
        for j in range(6):
            internal_sum = internal_sum + A[i, j] * (x[j] - P[i, j]) ** 2
        external_sum = external_sum + alpha[i] * np.exp(-internal_sum)

    return -external_sum

def Levy(x):
    # ref: http://www.sfu.ca/~ssurjano/levy.html
    x = np.array(x)
    w = [1.0 + (xi - 1.0) / 4.0 for xi in x]

    s0 = (math.sin(math.pi * w[0])) ** 2

    s1 = 0.0
    d = x.size
    for i in range(d - 1):
        s1 += (((w[i] - 1.0) ** 2) * (1.0 + 10.0 * (math.sin(math.pi * w[i] + 1.0)) ** 2))

    s2 = ((w[d - 1] - 1.0) ** 2) * (1.0 + (math.sin(2.0 * math.pi * w[d - 1])) ** 2)

    return -(s0 + s1 + s2)

def Rastrigin(x):
    #ref: http://www.sfu.ca/~ssurjano/rastr.html
    x = np.array(x)
    sum = 10.0 * x.size
    for xi in x:
        sum += ( xi**2 - 10.0 * math.cos(2.0*math.pi * xi)  )
    return -sum

def Hartmann6D(x):
    # ref https://www.sfu.ca/~ssurjano/hart6.html
    alpha = [1.0, 1.2, 3.0, 3.2];
    A = [[10, 3, 17, 3.5, 1.7, 8],
         [0.05, 10, 17, 0.1, 8, 14],
         [3, 3.5, 1.7, 10, 17, 8],
         [17, 8, 0.05, 10, 0.1, 14]]

    # 10^(-4) *
    P = [
        [1312, 1696, 5569, 124, 8283, 5886],
        [2329, 4135, 8307, 3736, 1004, 9991],
        [2348, 1451, 3522, 2883, 3047, 6650],
        [4047, 8828, 8732, 5743, 1091, 381]]

    outer = 0.0
    for ii in range(4):
        inner = 0.0
        for jj in range(6):
            xj = x[jj]
            Aij = A[ii][jj]
            Pij = P[ii][jj] * 1.0e-4
            inner = inner + Aij * (xj - Pij) ** 2;
        new = alpha[ii] * np.exp(-inner);
        outer = outer + new;

    y = -(2.58 + outer) / 1.94;
    return -y


"""

Path Planner with B-Spline

author: Atsushi Sakai (@Atsushi_twi)

"""

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as scipy_interpolate

def approximate_b_spline_path(x: list, y: list, n_path_points: int,
                              degree: int = 3) -> tuple:
    """
    approximate points with a B-Spline path

    :param x: x position list of approximated points
    :param y: y position list of approximated points
    :param n_path_points: number of path points
    :param degree: (Optional) B Spline curve degree
    :return: x and y position list of the result path
    """
    t = range(len(x))
    x_tup = scipy_interpolate.splrep(t, x, k=degree)
    y_tup = scipy_interpolate.splrep(t, y, k=degree)

    x_list = list(x_tup)
    x_list[1] = x + [0.0, 0.0, 0.0, 0.0]

    y_list = list(y_tup)
    y_list[1] = y + [0.0, 0.0, 0.0, 0.0]

    ipl_t = np.linspace(0.0, len(x) - 1, n_path_points)
    rx = scipy_interpolate.splev(ipl_t, x_list)
    ry = scipy_interpolate.splev(ipl_t, y_list)

    return rx, ry

def interpolate_b_spline_path(x: list, y: list, n_path_points: int,
                              degree: int = 3) -> tuple:
    """
    interpolate points with a B-Spline path

    :param x: x positions of interpolated points
    :param y: y positions of interpolated points
    :param n_path_points: number of path points
    :param degree: B-Spline degree
    :return: x and y position list of the result path
    """
    ipl_t = np.linspace(0.0, len(x) - 1, len(x))
    spl_i_x = scipy_interpolate.make_interp_spline(ipl_t, x, k=degree)
    spl_i_y = scipy_interpolate.make_interp_spline(ipl_t, y, k=degree)

    travel = np.linspace(0.0, len(x) - 1, n_path_points)
    return spl_i_x(travel), spl_i_y(travel)

def main():
    print(__file__ + " start!!")
    # way points
    way_point_x = [-1.0, 3.0, 4.0, 2.0, 1.0]
    way_point_y = [0.0, -3.0, 1.0, 1.0, 3.0]
    n_course_point = 100  # sampling number

    rax, ray = approximate_b_spline_path(way_point_x, way_point_y,
                                         n_course_point)
    rix, riy = interpolate_b_spline_path(way_point_x, way_point_y,
                                         n_course_point)

    # show results
    plt.plot(way_point_x, way_point_y, '-og', label="way points")
    plt.plot(rax, ray, '-r', label="Approximated B-Spline path")
    plt.plot(rix, riy, '-b', label="Interpolated B-Spline path")
    plt.grid(True)
    plt.legend()
    plt.axis("equal")
    plt.show()

if __name__ == '__main__':
    main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import math
import numpy as np

# Basic trigonometry
# -----------------------------------------------------------------

def normalize(v, tolerance=0.00001):
    mag2 = sum(n * n for n in v)
    if abs(mag2 - 1.0) > tolerance:
        mag = math.sqrt(mag2)
        if mag == 0.0:
            return None
        v = tuple(n / mag for n in v)
    return np.array(v)

def dot(v1, v2):
    n = 0
    lim = min( len(v1) , len(v2) )
    for i in range(lim):
        n += v1[i] * v2[i]
    return n

def length(v):
    return math.sqrt(sum(n * n for n in v))

def distance(A, B):
    ab = np.array(B) - np.array(A)
    return np.sqrt( ab[0] * ab[0] + ab[1] * ab[1] )

def perpendicular(A, B):
    ab = np.array(B) - np.array(A)
    dir_ab = normalize(ab)
    return np.array([-dir_ab[1], dir_ab[0]])

def rotate(xy, deg, anchor=[0, 0]):
    radians = math.radians(deg)
    x, y = xy

    x = x - anchor[0]
    y = y - anchor[1]
    cos_rad = math.cos(radians)
    sin_rad = math.sin(radians)
    qx = anchor[0] + cos_rad * x + sin_rad * y
    qy = anchor[1] + -sin_rad * x + cos_rad * y

    return np.array([qx, qy])

def clamp(value, min_value, max_value):
    return max(min(value, max_value), min_value)

def transform(xy, rotate = 0, scale = [1,1], translate = [0,0], anchor=[0, 0]):
    x, y = xy

    if scale != [1, 1] or rotate != 0:
        radians = math.radians(rotate)
        if isinstance(scale, tuple) or isinstance(scale, list):
            scale_x, scale_y = scale
        else:
            scale_x, scale_y = scale, scale

        x = x - anchor[0]
        y = y - anchor[1]

        cos_theta = math.cos(radians)
        sin_theta = math.sin(radians)
        nx = x * cos_theta - y * sin_theta
        ny = x * sin_theta + y * cos_theta

        nx *= scale_x
        ny *= scale_y

        nx += anchor[0]
        ny += anchor[1]
    else:
        nx = x
        ny = y

    nx += translate[0]
    ny += translate[1]
    return [nx, ny]

def remap(value, in_min, in_max, out_min, out_max):
    in_span = in_max - in_min
    out_span = out_max - out_min

    value = float(value - in_min)
    if value != 0:
        value /= float(in_span)
    return out_min + (value * out_span)

def lerp(A, B, t):
    A = np.array(A)
    B = np.array(B)
    return A * (1.0 - t) + B * t

# Path operations
# -----------------------------------------------------------------

def points_length(points):
    result = 0
    for (x1, y1), (x2, y2) in zip(points, points[1:]):
        result += math.hypot(x2 - x1, y2 - y1)
    return result

def path_length(path):
    return sum([points_length(path) for path in path], 0)

def join_path(path, tolerance):
    if len(path) < 2:
        return path
    result = [list(path[0])]
    for path in path[1:]:
        x1, y1 = result[-1][-1]
        x2, y2 = path[0]
        d = hypot(x2 - x1, y2 - y1)
        if d <= tolerance:
            result[-1].extend(path)
        else:
            result.append(list(path))
    return result

# Convertions
# -----------------------------------------------------------------

def polar2xy(center, angle, radius):
    a = math.radians(angle)
    rx = 1.0
    ry = 1.0
    if isinstance(radius, tuple) or isinstance(radius, list):
        rx = radius[0]
        ry = radius[1]
    else:
        rx = radius
        ry = radius
    return np.array([   center[0] + rx * math.cos(a),
                        center[1] + ry * math.sin(a)    ])

def xy2polar(center, pos):
    ab = np.array(pos) - np.array(center)

    dist = np.sqrt( ab[0] * ab[0] + ab[1] * ab[1] )

    angle = math.atan2(ab[1], ab[0])
    return [math.degrees(angle), dist]

# Bezier controls
# -----------------------------------------------------------------

def lineProp( pointA, pointB ):
    lengthX = pointB[0] - pointA[0]
    lengthY = pointB[1] - pointA[1]
    return math.sqrt( math.pow(lengthX, 2) + math.pow(lengthY, 2)), math.atan2(lengthY, lengthX)

def controlPoint( current, _previous, _next, reverse ):
    p = _previous
    n = _next

    if _previous is None:
        p = current
    if _next is None:
        n = current

    smoothing = 0.2
    o = lineProp(p, n)

    angle = o[1]
    if reverse:
        angle += math.pi
    length = o[0] * smoothing

    x = current[0] + math.cos(angle) * length
    y = current[1] + math.sin(angle) * length

    return [x, y]

def bezierCommand( point, i, a ):
    #  start control point
    cpsX, cpsY = controlPoint(a[i - 1], a[i - 2], point, False)

    #  end control point
    if i + 1 >= len(a):
        cpeX, cpeY = controlPoint(point, a[i - 1], None, True)
    else:
        cpeX, cpeY = controlPoint(point, a[i - 1], a[i + 1], True)

    return ' C ' + str(cpsX) + ',' + str(cpsY) + " " + str(cpeX) + "," + str(cpeY) + " " + str(point[0]) + "," + str(point[1])

# Collisions

#  https://github.com/openframeworks/openFrameworks/blob/patch-release/libs/openFrameworks/graphics/ofPolyline.inl#L622
def pointInside( pos, points ):
    counter = 0

    # double xinters;
    p1 = [0.0, 0.0]
    p2 = [0.0, 0.0]

    N = len(points)

    p1 = points[0]

    for i in range(1, N + 1):
        p2 = points[i % N]

        if pos[1] > min(p1[1], p2[1]):
            if pos[1] <= max(p1[1], p2[1]):
                if pos[0] <= max(p1[0], p2[0]):
                    if p1[1] != p2[1]:
                        xinters = (pos[1] - p1[1]) * (p2[0] - p1[0]) / (p2[1] - p1[1]) + p1[0]
                        if p1[0] == p2[0] or pos[0] <= xinters:
                            counter += 1
        p1 = p2

    return counter % 2 != 0

# https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/math/ofMath.h#L435
def linesIntersection(line1Start, line1End, line2Start, line2End):
    intersection = [0.0, 0.0]
    diffLA = np.array(line1End) - np.array(line1Start)
    compareA = diffLA[0] * line1Start[1] - diffLA[1] * line1Start[0]
    if ( ( diffLA[0]*line2Start[1] - diffLA[1]*line2Start[0] ) < compareA ) ^ ( ( diffLA[0]*line2End[1] - diffLA[1]*line2End[0] ) < compareA ):

        diffLB = np.array(line2End) - np.array(line2Start)
        compareB = diffLB[0] * line2Start[1] - diffLB[1] * line2Start[0]
        if ( ( diffLB[0]*line1Start[1] - diffLB[1]*line1Start[0] ) < compareB ) ^ ( ( diffLB[0]*line1End[1] - diffLB[1]*line1End[0]) < compareB ):
            lDetDivInv = 1 / ((diffLA[0] * diffLB[1]) - (diffLA[1] * diffLB[0]))
            intersection[0] =  -((diffLA[0] * compareB) - (compareA * diffLB[0])) * lDetDivInv
            intersection[1] =  -((diffLA[1] * compareB) - (compareA * diffLB[1])) * lDetDivInv
            return intersection

    return None

# PARSING SVGs files

def dom2dict(element):
    """Converts DOM elements to dictionaries of attributes."""
    keys = list(element.attributes.keys())
    values = [val.value for val in list(element.attributes.values())]
    return dict(list(zip(keys, values)))

#  From Andy Port's svgpathtools
# https://github.com/mathandy/svgpathtools/blob/master/svgpathtools/parser.py#L221
#
def _parse_transform_substr(transform_substr):

    type_str, value_str = transform_substr.split('(')
    value_str = value_str.replace(',', ' ')
    values = list(map(float, filter(None, value_str.split(' '))))

    transform = np.identity(3)
    if 'matrix' in type_str:
        if not _check_num_parsed_values(values, [6]):
            return transform

        transform[0:2, 0:3] = np.array([values[0:6:2], values[1:6:2]])

    elif 'translate' in transform_substr:
        if not _check_num_parsed_values(values, [1, 2]):
            return transform

        transform[0, 2] = values[0]
        if len(values) > 1:
            transform[1, 2] = values[1]

    elif 'scale' in transform_substr:
        if not _check_num_parsed_values(values, [1, 2]):
            return transform

        x_scale = values[0]
        y_scale = values[1] if (len(values) > 1) else x_scale
        transform[0, 0] = x_scale
        transform[1, 1] = y_scale

    elif 'rotate' in transform_substr:
        if not _check_num_parsed_values(values, [1, 3]):
            return transform

        angle = values[0] * np.pi / 180.0
        if len(values) == 3:
            offset = values[1:3]
        else:
            offset = (0, 0)
        tf_offset = np.identity(3)
        tf_offset[0:2, 2:3] = np.array([[offset[0]], [offset[1]]])
        tf_rotate = np.identity(3)
        tf_rotate[0:2, 0:2] = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
        tf_offset_neg = np.identity(3)
        tf_offset_neg[0:2, 2:3] = np.array([[-offset[0]], [-offset[1]]])

        transform = tf_offset.dot(tf_rotate).dot(tf_offset_neg)

    elif 'skewX' in transform_substr:
        if not _check_num_parsed_values(values, [1]):
            return transform

        transform[0, 1] = np.tan(values[0] * np.pi / 180.0)

    elif 'skewY' in transform_substr:
        if not _check_num_parsed_values(values, [1]):
            return transform

        transform[1, 0] = np.tan(values[0] * np.pi / 180.0)
    else:
        # Return an identity matrix if the type of transform is unknown, and warn the user
        warnings.warn('Unknown SVG transform type: {0}'.format(type_str))

    return transform

def parse_transform(el, transform_str):
    """Converts a valid SVG transformation string into a 3x3 matrix.
    If the string is empty or null, this returns a 3x3 identity matrix"""
    if not transform_str:
        return np.identity(3)
    elif not isinstance(transform_str, str):
        raise TypeError('Must provide a string to parse')

    transform_substrs = transform_str.split(')')[:-1]
    for transform_substr in transform_substrs:

        type_str, value_str = transform_substr.split('(')
        value_str = value_str.replace(',', ' ')
        values = list(map(float, filter(None, value_str.split(' '))))

        if 'translate' in transform_substr:
            x_translate = float(values[0])
            y_translate = float(values[1])
            el = el.getTranslated(x_translate, y_translate)

        elif 'scale' in transform_substr:
            x_scale = float(values[0])
            y_scale = float(values[1]) if (len(values) > 1) else x_scale
            el = el.getScaled(x_scale, y_scale)

        elif 'rotate' in transform_substr:
            deg = float(values[0])
            el = el.getRotated(deg)

        # elif 'skewX' in transform_substr:
        #     if not _check_num_parsed_values(values, [1]):
        #         return transform

        #     transform[0, 1] = np.tan(values[0] * np.pi / 180.0)

        # elif 'skewY' in transform_substr:
        #     if not _check_num_parsed_values(values, [1]):
        #         return transform

        #     transform[1, 0] = np.tan(values[0] * np.pi / 180.0)
        else:
            # Return an identity matrix if the type of transform is unknown, and warn the user
            warnings.warn('Unknown SVG transform type: {0}'.format(type_str))

    return el

#  COLOR

# stackoverflow.com/questions/214359/converting-hex-color-to-rgb-and-vice-versa
def hex2rgb(value):
    """Converts a hexadeximal color string to an RGB 3-tuple
    EXAMPLE
    -------
    >>> hex2rgb('#0000FF')
    (0, 0, 255)
    """
    value = value.lstrip('#')
    lv = len(value)
    return tuple(int(value[i:i+lv//3], 16) for i in range(0, lv, lv//3))

def rgb2gray(rgb):
    # print(rgb)
    r, g, b = rgb[0], rgb[1], rgb[2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b

    return gray

def hex2gray(value):
    return rgb2gray(hex2rgb(value))

def hex2fill(value):
    value = hex2gray(value)
    return value < 50.0


######################################################
#
# Generate Harmonics data
#
######################################################
import numpy as np

def generate(sineCoeffArray, sinePeriodsArray, cosineCoeffArray, cosinePeriodsArray, timeSteps, tStart = 0):

	if (len(sineCoeffArray) != len(sinePeriodsArray)):
		raise Exception('sineCoeffArray and sinePeriodsArray must be of the same length.')

	if (len(cosineCoeffArray) != len(cosinePeriodsArray)):
		raise Exception('cosineCoeffArray and cosinePeriodsArray must be of the same length.')

	outputArray = np.zeros(timeSteps)
	T = float(timeSteps)
	for i in range(tStart, timeSteps):
		value = 0.0
		for j in range(0, len(sineCoeffArray)):
			value += (sineCoeffArray[j] * np.sin(i * sinePeriodsArray[j] * 2.0 * np.pi / T ))

		for k in range(0, len(cosineCoeffArray)):
			value += (cosineCoeffArray[k] * np.cos(i * cosinePeriodsArray[k] * 2.0 * np.pi / T))

		outputArray[i] = value

	return outputArray


import numpy as np

def edge(size, frequency, angle, beta=0):
    '''Generate an image with periodic edges.

    Parameters
    ----------
    size : tuple
        width and height of the output image
    frequency : float
        the frequency of the pulse train, in cycles per pixel
    angle : float
        value in degrees indicating the orientation of the sinusoid
    beta : float
        value of the Kaiser window 'beta' parameter

    Returns
    -------
    numpy.ndarray
        the generated output image
    '''
    if frequency < 0:
        raise ValueError('Frequency must be a positive value.')

    omega = 2.0 * np.pi * frequency / np.min(size)
    theta = np.deg2rad(angle)

    y, x = np.meshgrid(np.arange(size[1]), np.arange(size[0]))
    u = x * np.cos(theta) + y * np.sin(theta)
    out = np.cos(omega*u)
    out[out >= 0] = 1
    out[out < 0] = 0

    return out


#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 21000, 1000)
r = np.log(0.5)
t1 = 5730
t2 = 1600
y1 = np.exp((r / t1) * x)
y2 = np.exp((r / t2) * x)

fig, ax = plt.subplots()
plt.plot(x, y1, 'r--', label='C-14')
plt.plot(x, y2, 'g', label='Ra-226')
plt.axis([0, 20000, 0.0, 1.0])
ax.set_title("Exponential Decay of Radioactive Elements", va='bottom')
plt.xlabel('Time (years)')
plt.ylabel('Fraction Remaining')
plt.legend(framealpha=1, frameon=True)
plt.show()


import numpy as np

hid_activations = ['relu', 'logsig', 'tanh']
out_activations = ['linear', 'softmax', 'logsig']

## general activations

def relu(X):
    return np.where(X > 0, X, 0)

def drelu(X):
    return np.where(X > 0, 1, 0)

def logsig(X):
    return 1 / (1 + np.exp(-X))

def dlogsig(X):
    return logsig(X) * (1 - logsig(X))

def tanh(X):
    return np.tanh(X)

def dtanh(X):
    return 1 / np.cosh(X) ** 2

## output-only activations

def linear(X):
    return X

def dlinear(X):
    return np.ones(X.shape)

def softmax(X):
    return (np.exp(X) / np.sum(np.exp(X)))

def dsoftmax(X):
    out = np.zeros(X.shape)
    out[np.argmax(X)] = np.exp(X[np.argmax(X)]) / np.sum(np.exp(X))
    return out

## cost functions

def MSE(D, Y):
    return np.sum((D - Y) ** 2, axis=0)

def CE(D, Y):
    return -np.sum(np.log(Y) * D, axis=0)


"""
Authors: Aditya Jain
Contact : https://adityajain.me
"""

import numpy as np

class KNeighborsClassifier():
	"""
	K Nearest Neighbors Classifier which classifies sample point based on majority of k nearby sample classes

	Parameters
	----------
	n_neighbors : integer (Default 5), number of neighbors to consider

	metric : str ( 'minkowski', 'euclidean', 'manhatten' ) (Default : 'minkowski')

	p : integer (Default 2), metric used in minkowski distance

	normalize : boolean, normalize data before calculating distance

	"""

	def __init__(self, n_neighbors=5, metric='minkowski', p=2, normalize=False):
		self.__n_neighbors = n_neighbors
		self.__X = None
		self.__y = None
		self.__normalize = normalize
		self.__metric = { 'minkowski': self.__minkowski, 'euclidean':self.__euclidean, 'manhatten':self.__manhatten }[metric]
		self.__p = p
		self.__n_classes = None
		self.__means = None
		self.__std = None

	def __euclidean(self,X1,X2):
		return np.sqrt(np.sum(np.square(X1-X2),axis=1))

	def __manhatten(self,X1,X2):
		return np.sum(np.abs(X1-X2),axis=1)

	def __minkowski(self,X1,X2):
		return np.power(np.sum(np.power(np.abs(X1-X2),self.__p),axis=1),1/self.__p)

	def __normalizeX(self,X):
		return (X-self.__means)/self.__std

	def fit(self,X,y):
		"""
		Fit X using y

		Parameters
		----------
		X : 2D numpy array, independent variables

		y : 1D numpy array, dependent variable

		"""
		self.__y, self.__n_classes = y, len(np.unique(y))
		if self.__normalize:
			self.__means, self.__std = X.mean(axis=0), X.std(axis=0)
			self.__X = self.__normalizeX(X)
		else:
			self.__X = X

	def predict_proba(self,X):
		"""
		Predict probability of all classes

		Parameters
		----------
		X : numpy array, independent variables

		Returns
		-------

		predicted probabilities

		"""
		if self.__normalize: X = self.__normalizeX(X)
		probs = []
		for sample in X:
			x = np.expand_dims(sample,axis=0)
			distances = self.__metric(self.__X, x)
			top_k_index = distances.argsort()[:self.__n_neighbors]
			prob = np.zeros(self.__n_classes)
			cls,cnts = np.unique(self.__y[top_k_index], return_counts=True)
			for cl,cn in zip(cls,cnts): prob[cl] = cn/sum(cnts)
			probs.append(prob)
		return np.array(probs)

	def predict(self,X):
		"""
		Predict dependent variable

		Parameters
		---------
		X : numpy array, independent variables

		Returns
		-------
		Predicted classes

		"""
		return np.argmax( self.predict_proba(X), axis=1 )

	def score(self,X,y):
		"""
		Calculate accuracy from independent variables

		Parameters
		----------
		X : numpy array, independent variables

		y : numpy array, dependent variable

		Returns
		-------
		accuracy score

		"""
		return (y==self.predict(X)).sum()/len(y)


import numpy as np
from sigmoid import *

def cost_function(theta, X, y): # 返回代价值，代价函数对应的斜率（导数）
    m = y.size

    # You need to return the following values correctly
    cost = 0
    grad = np.zeros(theta.shape)

    # ===================== Your Code Here =====================
    # Instructions : Compute the cost of a particular choice of theta
    #                You should set cost and grad correctly.
    #

    hypothesis = sigmoid(X @ theta.T)
    first = np.multiply(-y, np.log(hypothesis))
    second = np.multiply(1 - y, np.log(1-hypothesis))
    cost = np.sum(first - second) / m #代价

    grad = np.dot((hypothesis - y), X) / m    #代价函数对应点的斜率（导数）
    # ===========================================================

    return cost, grad


"""
Function module
"""
from dataclasses import dataclass, field
from typing import Callable

import numpy as np

@dataclass
class SmoothFunction:
    name: str
    fun: Callable = field(repr=False)
    dfun: Callable = field(repr=False)
    d2fun: Callable = field(default=None, repr=False)

fun_list = [
    SmoothFunction(name="identity",
                   fun=lambda x: x,
                   dfun=lambda x: 1.0 if np.isscalar(x) else np.ones(len(x)),
                   d2fun=lambda x: 0.0 if np.isscalar(x) else np.zeros(len(x))),
    SmoothFunction(name="quad",
                   fun=lambda x: x**2,
                   dfun=lambda x: 2*x,
                   d2fun=lambda x: 2.0 if np.isscalar(x) else np.full(len(x), 2.0)),
    SmoothFunction(name="exp",
                   fun=np.exp,
                   dfun=np.exp,
                   d2fun=np.exp),
    SmoothFunction(name="expit",
                   fun=lambda x: 1/(1 + np.exp(-x)),
                   dfun=lambda x: np.exp(-x)/(1 + np.exp(-x))**2,
                   d2fun=lambda x: np.exp(-x)*(np.exp(-x) - 1)/(np.exp(-x) + 1)**3),
    SmoothFunction(name="log",
                   fun=np.log,
                   dfun=lambda x: 1/x,
                   d2fun=lambda x: -1/x**2),
    SmoothFunction(name="logit",
                   fun=lambda x: np.log(x/(1 - x)),
                   dfun=lambda x: 1/(x*(1 - x)),
                   d2fun=lambda x: (2*x - 1)/(x*(1 - x))**2),
]

fun_dict = {
    fun.name: fun
    for fun in fun_list
}


import tensorflow.compat.v1 as tf
import numpy as np

#generates a random square orthogonal matrix of dimension dim
def square_orthogonal_matrix(dim=3, seed=None):
     random_state = np.random
     if seed is not None: #allows to get the same matrix every time
        random_state.seed(seed)
     H = np.eye(dim)
     D = np.ones((dim,))
     for n in range(1, dim):
         x = random_state.normal(size=(dim-n+1,))
         D[n-1] = np.sign(x[0])
         x[0] -= D[n-1]*np.sqrt((x*x).sum())
         # Householder transformation
         Hx = (np.eye(dim-n+1) - 2.*np.outer(x, x)/(x*x).sum())
         mat = np.eye(dim)
         mat[n-1:, n-1:] = Hx
         H = np.dot(H, mat)
         # Fix the last sign such that the determinant is 1
     D[-1] = (-1)**(1-(dim % 2))*D.prod()
     # Equivalent to np.dot(np.diag(D), H) but faster, apparently
     H = (D*H.T).T
     return H

#generates a random (semi-)orthogonal matrix of size NxM
def semi_orthogonal_matrix(N, M, seed=None):
    if N > M: #number of rows is larger than number of columns
        square_matrix = square_orthogonal_matrix(dim=N, seed=seed)
    else: #number of columns is larger than number of rows
        square_matrix = square_orthogonal_matrix(dim=M, seed=seed)
    return square_matrix[:N,:M]

#generates a weight matrix with variance according to Glorot initialization
#based on a random (semi-)orthogonal matrix
#neural networks are expected to learn better when features are decorrelated
#(stated by eg. "Reducing overfitting in deep networks by decorrelating representations",
#"Dropout: a simple way to prevent neural networks from overfitting",
#"Exact solutions to the nonlinear dynamics of learning in deep linear neural networks")
def semi_orthogonal_glorot_weights(n_in, n_out, scale=2.0, seed=None):
    W = semi_orthogonal_matrix(n_in, n_out)
    W *= np.sqrt(scale/((n_in+n_out)*W.var()))
    return W

#given a tensor x, gives back the variance
def reduce_var(x, axis=None, keepdims=False):
    """Variance of a tensor, alongside the specified axis.

    # Arguments
        x: A tensor or variable.
        axis: An integer, the axis to compute the variance.
        keepdims: A boolean, whether to keep the dimensions or not.
            If `keepdims` is `False`, the rank of the tensor is reduced
            by 1. If `keepdims` is `True`,
            the reduced dimension is retained with length 1.

    # Returns
        A tensor with the variance of elements of `x`.
    """

    m = tf.reduce_mean(input_tensor=x, axis=axis, keepdims=True)
    devs_squared = tf.square(x - m)
    return tf.reduce_mean(input_tensor=devs_squared, axis=axis, keepdims=keepdims)


# Monte Carlo valuation of European call options with pure Python
# mcs_pure_python.py
# https://github.com/yhilpisch/py4fi

from time import time
import numpy as np

np.random.seed(1234)
t0 = time()

# Parameters
S0 = 100.  # initial value
K = 105.  # strike price
T = 1.0  # maturity
r = 0.05  # risk-less short rate
sigma = 0.2  # volatility
M = 50  # number of time steps
dt = T / M  # length of time interval
n_paths = 250000  # number of paths

# Simulating n_paths paths with M time steps
S = []
for i in range(n_paths):
    path = []
    for t in range(M + 1):
        if t == 0:
            path.append(S0)
        else:
            z = np.random.standard_normal()
            St = path[t - 1] * np.exp((r - 0.5 * sigma ** 2) * dt + sigma * np.sqrt(dt) * z)
            path.append(St)
    S.append(path)

# Calculating the Monte Carlo estimator
C0 = np.exp(-r * T) * np.sum([np.maximum(path[-1] - K, 0) for path in S]) / n_paths

# Results output
tpy = time() - t0
print("European Option Value %7.3f" % C0)
print("Duration in Seconds   %7.3f" % tpy)

03 Montecarlo valuation of European call - pure Python.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Created by igor on 18/10/2017
import numpy as np

#Activation functions
sigmoid = {
    'fn': lambda z: 1 / (1+np.exp(-z)),
    'der': lambda z: z*(1-z)
}

relu = {
    'fn': lambda z:z if z>0 else 0,
    'der': lambda z: 1 if z>0 else 0
}

#Error functions
errorMeanSq = {
    'fn': lambda out, ans: 0.5 * np.power(out-ans,2),
    'der': lambda out, ans: out - ans
}


# -*- coding: utf-8 -*-
"""
Plot the interpolation functions for a 4-node isoparametric
element.

@author: Nicolas Guarin-Zapata
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

Axes3D
plt.rcParams["mathtext.fontset"] = "cm"

def make_plot(x, y, N, cont):
    x_cords = [-1, 1, 1, -1]
    y_cords = [-1, -1, 1, 1]

    ax = plt.gca()
    ax.plot([-1, 1, 1, -1, -1], [-1, -1, 1, 1, -1], "-ko", zorder=-10)
    ax.plot([x_cords[cont], x_cords[cont]],
            [y_cords[cont], y_cords[cont]], [0, 1], "--k", zorder=-10)
    ax.plot_surface(x, y, N, cstride=1, rstride=1, cmap="viridis",
                alpha=0.6, lw=0.3, zorder=3, edgecolors="black")
    ax.view_init(azim=-60, elev=30)
    ax.set_xlabel(r"$x$", fontsize=12)
    ax.set_ylabel(r"$y$", fontsize=12)
    ax.set_zlabel(r"$N_%i(x, y)$"%(cont + 1), fontsize=12)
    ax.set_xticks([-1, 0, 1])
    ax.set_yticks([-1, 0, 1])
    ax.set_zticks([0.0, 0.5, 1.0])

x, y = np.mgrid[-1:1:11j, -1:1:11j]
N1 = 0.25*(1 - x)*(1 - y)
N2 = 0.25*(1 + x)*(1 - y)
N3 = 0.25*(1 + x)*(1 + y)
N4 = 0.25*(1 - x)*(1 + y)

plt.close("all")
cont = 0
fig = plt.figure(figsize=(5, 5))
for cont, N in enumerate([N1, N2, N3, N4]):
    ax = fig.add_subplot(2, 2, cont + 1, projection='3d')
    make_plot(x, y, N, cont)

plt.savefig("interp_4-nodos.pdf", transparent=True)
plt.show()


import numpy as np

def make_2D_Gaussian(size, fwhm=3):
    """make a 2D Gaussian kernel.

    find slope of the phase plane through
    two point step size for phase correlation minimization

    Parameters
    ----------
    size : list, size=(1,2), dtype=integer
        size of the resulting numpy array
    fwhm : float, size=(2,1)
        a.k.a.: full-width-half-maximum
        can be thought of as an effective radius

    Returns
    -------
    M : np.array, size=(size,size)
        array with Gaussian peak in the center

    See Also
    --------
    phase_svd, phase_radon, phase_difference

    Notes
    -----
    .. math:: M = e^{ -4 \ln(2) \frac{x^2 + y^2}{fwhm}}
    """
    if len(size)==1:
        size = (size, size)

    x = np.linspace(-(size[0]-1)/2,+(size[0]-1)/2, size[0], float)
    y = np.linspace(-(size[1]-1)/2,+(size[1]-1)/2, size[0], float)
    y = y[:,np.newaxis]
    M = np.exp(-4*np.log(2) * (x**2 + y**2) / fwhm**2)
    return M

def make_2D_Laplacian(alpha=.5):
    alpha = max(0, min(alpha, 1))
    cross_weight = np.divide(1.-alpha, 1.+alpha)
    diag_weight = np.divide(alpha, (alpha + 1.))
    center = np.divide(-4., 1.+alpha)
    M = np.array([
        [diag_weight, cross_weight, diag_weight],
        [cross_weight, center, cross_weight],
        [diag_weight, cross_weight, diag_weight]])
    return M

def sigma_filtering(y,thres=3):
    """ 3-sigma filtering

    finds the traditional statistics and filters the 1% outliers

    Parameters
    ----------
    y : np.array, size=(_,1), dtype=float
        data of interest
    thres : float, size=1
        multiplication factor of the sigma
        1: 68%
        2: 95%
        3: 99%

    Returns
    -------
    IN : np.array, size=(_,1) , dtype=boolean
        array with classification

    See Also
    --------
    weighted_sigma_filtering, mad_filtering
    """
    mean_y = np.mean(y)
    std_y = np.std(y)

    IN = (y > (mean_y - thres*std_y)) & \
        (y < mean_y + thres*std_y)
    return IN

def weighted_sigma_filtering(y,w,thres=3):
    """ weighted 3-sigma filtering

    finds the weighted statistics and filters with these statistics

    Parameters
    ----------
    y : np.array, size=(_,1), dtype=float
        data of interest
    w : np.array, size=(_,1), dtype=float
        weights to be used for the statistics calculation
    thres : float, size=1
        multiplication factor of the sigma
        1: 68%
        2: 95%
        3: 99%

    Returns
    -------
    IN : np.array, size=(_,1) , dtype=boolean
        array with classification

    See Also
    --------
    sigma_filtering, mad_filtering
    """
    mean_y = np.average(y,weights=w)
    std_y = math.sqrt(np.average((y-mean_y)**2, weights=w))

    IN = (y > (mean_y - thres*std_y)) & \
        (y < mean_y + thres*std_y)
    return IN

def mad_filtering(y,thres=1.4826):
    """ robust 3-sigma filtering

    finds the robust statistics and filters with these statistics

    Parameters
    ----------
    y : np.array, size=(_,1), dtype=float
        data of interest
    thres : float, size=1
        multiplication factor of the median absolute difference.
        if the distribution is normal then 1.4826*thresh = sigma

    Returns
    -------
    IN : np.array, size=(_,1) , dtype=boolean
        array with classification

    See Also
    --------
    sigma_filtering, weighted_sigma_filtering,
    huber_filtering, cauchy_filtering
    """
    med_y = np.median(y)
    mad_y = np.median(np.abs(y - med_y))

    IN = (y > (med_y - thres*mad_y)) & \
        (y < med_y + thres*mad_y)
    return IN

def normalize_variance(y):
    """ normalize data through its variance

    project/scale data towards a uniform space,
    by means of classical statistics

    Parameters
    ----------
    y : np.array, size=(_,1), dtype=float
        data of interest

    Returns
    -------
    r : np.array, size=(_,1) , dtype=float
        projected data, centered around the origin

    See Also
    --------
    normalize_variance_robust
    """
    mean_y = np.mean(y)
    std_y = np.std(y)
    r = (y-mean_y)/std_y
    return r

def normalize_variance_robust(y):
    """ normalize data through its robust statistics

    project/scale data towards a uniform space,
    by means of robust statistics

    Parameters
    ----------
    y : np.array, size=(_,1), dtype=float
        data of interest

    Returns
    -------
    r : np.array, size=(_,1) , dtype=float
        projected data, centered around the origin

    See Also
    --------
    normalize_variance
    """
    med_y = np.median(y)
    mad_y = np.median(np.abs(med_y - med_y))
    r = (y-med_y)/mad_y
    return r

def huber_filtering(y,thres=1.345,preproc='normal'):
    """ filter with statitics following a Huber function

    finds the robust statistics and filters with these statistics

    Parameters
    ----------
    y : np.array, size=(_,1), dtype=float
        data of interest
    thres : float, size=1
        multiplication factor of the median absolute difference.
        if the distribution is normal then 1.345*thresh = sigma
    preproc: string, default='normal'
        'normal'
            using classical statistics to estimate the mean
        else:
            using robust statistics

    Returns
    -------
    IN : np.array, size=(_,1) , dtype=boolean
        array with classification

    See Also
    --------
    sigma_filtering, weighted_sigma_filtering,
    mad_filtering, cauchy_filtering
    """
    if preproc == 'normal':
        r = normalize_variance(y)
    else:
        r = normalize_variance_robust(y)
    w = 1 / max(1, abs(r))
    IN = w<thres
    return IN

def cauchy_filtering(y,thres=2.385,preproc='normal'):
    """ filter with statitics following a Cauchy function

    finds the robust statistics and filters with these statistics

    Parameters
    ----------
    y : np.array, size=(_,1), dtype=float
        data of interest
    thres : float, size=1
        multiplication factor of the median absolute difference.
        if the distribution is normal then 2.385*thresh = sigma
    preproc: string, default='normal'
        'normal'
            using classical statistics to estimate the mean
        else:
            using robust statistics

    Returns
    -------
    IN : np.array, size=(_,1) , dtype=boolean
        array with classification

    See Also
    --------
    sigma_filtering, weighted_sigma_filtering,
    mad_filtering, huber_filtering
    """
    if preproc == 'normal':
        r = normalize_variance(y)
    else:
        r = normalize_variance_robust(y)
    w = 1 / (1 + r**2)
    IN = w<thres
    return IN

def normalized_sampling_histogram(I):
    """ sample histogram based upon Sturges' rule

    Parameters
    ----------
    I : np.array, dim={1,n}, dtype={integer,float}

    Returns
    -------
    values : np.array, size=(m)
        occurances within an interval
    base : np.array, size=(m)
        central value of the interval

    References
    ----------
    .. [1] Sturges, "The choice of a class interval". Journal of the american
       statistical association. vol.21(153) pp.65–66.
    """
    sturge = 1.6 * (np.log2(I.size) + 1)
    values, base = np.histogram(I.flatten(),
                                bins=int(np.ceil(sturge)))
    # transform to centers
    base = base[:-1] + np.diff(base)
    return values, base


import numpy as np

def quad_function(point, p, store_x0, matrix_test):
    """
    Minimum of several Quadratic forms function.

    Parameters
    ----------
    point : 1-D array with shape (d, )
            A point used to evaluate the function.
    p : integer
        Number of local minima.
    store_x0 : 2-D array with shape (p, d).
    matrix_test : 3-D array with shape (p, d, d).

    Returns
    -------
    value_of_minimum : float
                       Function value.
    """
    d = point.shape[0]
    store_func_values = (np.transpose((point - store_x0).reshape(p, d, 1),
                                      (0, 2, 1)) @
                         matrix_test @ (point - store_x0).reshape(p, d, 1))
    value_of_minimum = np.min(store_func_values)
    return value_of_minimum

def quad_gradient(point, p, store_x0, matrix_test):
    """
    Minimum of several Quadratic forms gradient.

    Parameters
    ----------
    point : 1-D array with shape (d, )
            A point used to evaluate the gradient.
    p : integer
        Number of local minima.
    store_x0 : 2-D array with shape (p, d).
    matrix_test : 3-D array with shape (p, d, d).

    Returns
    -------
    gradient : 1-D array with shape (d, )
               Gradient at point.
    """
    d = point.shape[0]
    store_func_values = (np.transpose((point - store_x0).reshape(p, d, 1),
                                      (0, 2, 1)) @
                         matrix_test @ (point - store_x0).reshape(p, d, 1))
    position_minimum = np.argmin(store_func_values)
    gradient = 2 * (matrix_test[position_minimum] @
                    (point - store_x0[position_minimum]))
    return gradient

def calc_minimizer_quad(point, p, store_x0, matrix_test):
    """
    Finding the position of the local minimizer which point is closest
    to, using the minimum of several Quadratic forms function.

    Parameters
    ----------
    point : 1-D array with shape (d, )
            A point used to evaluate the function.
    p : integer
        Number of local minima.
    store_x0 : 2-D arrays with shape (p, d).
    matrix_test : 3-D arrays with shape (p, d, d).

    Returns
    -------
    position_minimum : integer
                       Position of the local minimizer which produces the
                       smallest distance between point and all p local
                       minimizers.
    """
    d = point.shape[0]
    store_func_values = (np.transpose((point - store_x0).reshape(p, d, 1),
                                      (0, 2, 1)) @
                         matrix_test @ (point - store_x0).reshape(p, d, 1))
    position_minimum = np.argmin(store_func_values)
    norm_with_minimizer = np.linalg.norm(point - store_x0[position_minimum])
    assert(norm_with_minimizer < 0.2)
    return position_minimum


import numpy as np
import sys

def deGroot(environment, beliefs):
    i = 0
    consensus = False
    while (consensus == False and i < 1000):
        old_beliefs = beliefs
        beliefs = environment @ beliefs
        #if (np.all(old_beliefs - beliefs == np.zeros(len(beliefs)))):
        if (np.all(old_beliefs - beliefs < np.full(len(beliefs),sys.float_info.epsilon))):
            consensus = True
        i = i + 1
    return beliefs

def deGrootSteps(environment, beliefs):
    steps = []
    i = 0
    consensus = False
    while (consensus == False and i < 1000):
        old_beliefs = beliefs
        beliefs = environment @ beliefs
        #if (np.all(old_beliefs - beliefs == np.zeros(len(beliefs)))):
        if (np.all(old_beliefs - beliefs < np.full(len(beliefs),sys.float_info.epsilon))):
            consensus = True
        i = i + 1
        steps.append(beliefs)
    return steps

#print(deGroot(T, p0))


from numpy import zeros

# Define ab2 function
def ab2(f,t0,tf,y0,n):
    h = (tf - t0)/n
    t = zeros(n+1)
    y = zeros(n+1)
    t[0] = t0
    y[0] = y0
    y[1] = y[0] + h * f(t[0],y[0])
    t[1] = t[0] + h
    for i in range(1,n):
        y[i+1] = y[i] + (3.0/2.0) * h * f(t[i],y[i])-1.0/2.0 * h * f(t[i-1],y[i-1])
        t[i+1] = t[i] + h
    return t,y

# Define functions
def f(t,y):
    return  t - y

# Set initial conditions
t0 = 0.0
tf = 1.0
y0 = 1.0
n = 5

# Execute AB2
t, yab2 = ab2(f,t0,tf,y0,n)

# Print results
print("%5s %8s" % ('t','y'))
for i in range(n+1):
    print("%8.4f %8.4f" % (t[i],yab2[i]))


 # Imports

import numpy as np

# Coordinates:
x1, y1 = 3, 4
x2, y2 = 0, 0
x3, y3 = 3, 0

# Member lengths

# Member 1 (btw nodes 2 and 3)
len1 = np.linalg.norm(np.array((x2, y2)) - np.array((x3, y3)))

# Member 2 (btw nodes 2 and 1)
len2 = np.linalg.norm(np.array((x2, y2)) - np.array((x1, y1)))

print('Length 1:', len1, '\nLength 2:', len2)

def calc_length(xy1: tuple, xy2: tuple) -> float:
    """ Given the coordinates of two points, it will calculate the
    distance between the two points.
    """
    return np.linalg.norm(np.array(xy1) - np.array(xy2))

len1 = calc_length((x2, y2), (x3, y3))
len2 = calc_length((x1, y1), (x2, y2))

print('Length 1:', len1, '\nLength 2:', len2)

def dir_cos(xy1, xy2):
    """ Returns the direction cosine for a member given point coordinates.
    """
    l = calc_length(xy1, xy2)
    return (xy2[0] - xy1[0])/l, (xy2[1] - xy1[1])/l

lx1, ly1 = dir_cos((x2, y2), (x3, y3))
lx2, ly2 = dir_cos((x2, y2), (x1, y1))

print(lx1, ly1, lx2, ly2)

def stiff_matrix(xy1, xy2):
    """ Returns the stiffness matrix for a member, given the coordinates of
    its near and far end nodes.
    """
    length = calc_length(xy1, xy2)
    lx, ly = dir_cos(xy1, xy2)
    return np.array(
        [
        [lx**2, lx*ly, -lx**2, -lx*ly],
        [lx*ly, ly**2, -lx*ly, -ly**2],
        [-lx**2, -lx*ly, lx**2, lx*ly],
        [-lx*ly, -ly**2, lx*ly, ly**2]
        ]
    ) / length

k1 = stiff_matrix((x2, y2), (x3, y3))
k2 = stiff_matrix((x2, y2), (x1, y1))

print(k1, '\n')
print(k2)

def stiff_matrix(xy1, xy2, other_dofs=None):
    """ Returns the stiffness matrix for a member, given the coordinates of
    its near and far end nodes. Can optionally provide a tuple that includes
    other degrees of freedom in order to compile a matrix corresponding
    to the entire system.
    """
    length = calc_length(xy1, xy2)
    lx, ly = dir_cos(xy1, xy2)
    k = np.array(
        [
        [lx**2, lx*ly, -lx**2, -lx*ly],
        [lx*ly, ly**2, -lx*ly, -ly**2],
        [-lx**2, -lx*ly, lx**2, lx*ly],
        [-lx*ly, -ly**2, lx*ly, ly**2]
        ]
    ) / length

    if other_dofs:
        for i in other_dofs:
            k = np.insert(k, i-1, 0, axis=0)
            k = np.insert(k, i-1, 0, axis=1)

    return k

k1 = stiff_matrix((x2, y2), (x3, y3), (5, 6, ))
k2 = stiff_matrix((x2, y2), (x1, y1), (3, 4, ))

print(k1, '\n')
print(k2)


import numpy as np
import random

def MeanSquaredError(real, predicted):
    if len(real) != len(predicted):
        raise Exception("Input size not equal")
    size = len(real)
    summ = 0.0
    for i in range(size):
        summ += (real - predicted)**2
    return summ / size

def RidgeCost(real, predicted, alpha):
    if len(real) != len(predicted):
        raise Exception("Input size not equal")
    size = len(real)
    mse = MeanSquaredError(real, predicted)
    return (alpha * 0.5 * size + 1) * mse

class LinearRegression:
    def __init__(self, alpha, gradient_type="Full", batch_size = 0,  n_iters = 1000):
        self.alpha = alpha
        self.gradient_type = gradient_type
        self.batch_size = batch_size
        self.n_iters = n_iters
        self.bias = 0.0
        self.coeff = None
    def fit(self, X, y):
        n_samples, n_features = np.shape(X)
        self.coeff = np.zeros(n_features)
        X = np.array(X)
        y = np.array(y)
        XT = np.transpose(X)
        if self.gradient_type == "Full":
            for _ in range(self.n_iters):
                y_predicted = np.dot(X, self.coeff) + self.bias

                dw = (1 / n_samples) * (2 * np.dot(XT, (y_predicted - y)))
                db = (1/n_samples) * (2 * np.sum(y_predicted - y))

                self.coeff -= self.alpha * dw
                self.bias -= self.alpha * db
        elif self.gradient_type == "Stochastic":
            for _ in range(self.n_iters):
                rand_i = random.randint(0, n_samples-1)
                XT_rand = [[XT[i][rand_i]] for i in range(n_features)]
                y_predicted = np.dot([X[rand_i]], self.coeff) + self.bias

                dw = (2 * np.dot(XT_rand, (y_predicted - y[rand_i])))
                db = (2 * np.sum(y_predicted - y[rand_i]))

                self.coeff -= self.alpha * dw
                self.bias -= self.alpha * db
        elif self.gradient_type == "Mini":
            if self.batch_size > n_samples:
                raise Exception('Batch size cannot be larger than number of samples')
            for _ in range(self.n_iters):
                XT_bat = [XT[i][:self.batch_size] for i in range(n_features)]
                y_predicted = np.dot(X[:self.batch_size], self.coeff) + self.bias

                dw = (1 / self.batch_size) * (2 * np.dot(XT_bat, (y_predicted - y[:self.batch_size])))
                db = (1 / self.batch_size) * (2 * np.sum(y_predicted - y[:self.batch_size]))

                self.coeff -= self.alpha * dw
                self.bias -= self.alpha * db

    def predict(self, X):
        return np.dot(X, self.coeff) + self.bias


from __future__ import absolute_import
import numpy as np
from pyti import catch_errors
from pyti.function_helper import fill_for_noncomputable_vals
from six.moves import range
from six.moves import zip

def relative_strength_index(data, period):
    """
    Relative Strength Index.

    Formula:
    RSI = 100 - (100 / 1 + (prevGain/prevLoss))
    """
    catch_errors.check_for_period_error(data, period)

    period = int(period)
    changes = [data_tup[1] - data_tup[0] for data_tup in zip(data[::1], data[1::1])]

    filtered_gain = [val < 0 for val in changes]
    gains = [0 if filtered_gain[idx] is True else changes[idx] for idx in range(0, len(filtered_gain))]

    filtered_loss = [val > 0 for val in changes]
    losses = [0 if filtered_loss[idx] is True else abs(changes[idx]) for idx in range(0, len(filtered_loss))]

    avg_gain = np.mean(gains[:period])
    avg_loss = np.mean(losses[:period])

    rsi = []
    if avg_loss == 0:
        rsi.append(100)
    else:
        rs = avg_gain / avg_loss
        rsi.append(100 - (100 / (1 + rs)))

    for idx in range(1, len(data) - period):
        avg_gain = ((avg_gain * (period - 1) +
                    gains[idx + (period - 1)]) / period)
        avg_loss = ((avg_loss * (period - 1) +
                    losses[idx + (period - 1)]) / period)

        if avg_loss == 0:
            rsi.append(100)
        else:
            rs = avg_gain / avg_loss
            rsi.append(100 - (100 / (1 + rs)))

    rsi = fill_for_noncomputable_vals(data, rsi)

    return rsi


import numpy as np
import matplotlib.pyplot as plt

def m_splines_basis(x, M, K):
    """Return the basis matrix for order-M splines."""
    dof = K + M
    xi = np.linspace(np.min(x), np.max(x), K+2)[1:-1]
    H = np.zeros((len(x), dof))
    for j in range(M):
        H[:,j] = np.power(x, j)
    for k in range(K):
        H[:,M+k] = np.where((x - xi[k]) > 0, np.power(x - xi[k], M-1), 0)
    return H

def m_splines(x, M=4, K=8):
    H = m_splines_basis(x, M, K)
    bhat = np.linalg.inv(H.T @ H) @ (H.T @ y)
    yhat = H @ bhat
    return yhat

def plot_mean_function(x, y, yhat):
    plt.figure(figsize=(10,6))
    plt.scatter(x, y, s=20, label='Mean sample signal')
    plt.plot(x, yhat, c='red', alpha=0.7, label='Estimated mean function')
    plt.xlabel(r'$x$')
    plt.ylabel(r'$y$')
    plt.legend(fontsize=14)
    plt.show()
    return None

def tau_knot_sequence(x, K, M):
    a = np.min(x)
    b = np.max(x)+1e-8
    t_inner = np.linspace(a, b, K+2)[1:-1]
    t_left = np.array([a]*M)
    t_right = np.array([b]*M)
    tau = np.append(t_left, t_inner)
    tau = np.append(tau, t_right)
    return tau

def b_basis(x, t, m, j):
    if m == 0:
        if t[j] <= x < t[j+1]:
            return 1.0
        else:
            return 0.0
    elif t[j+m] == t[j]:
        a = 0.0
    else:
        a = (x - t[j]) / (t[j+m] - t[j]) * b_basis(x, t, m-1, j)
    if t[j+m+1] == t[j+1]:
        b = 0.0
    else:
        b = (t[j+m+1] - x) / (t[j+m+1] - t[j+1]) * b_basis(x, t, m-1, j+1)
    return a + b

def b_splines_basis(x, t, m):
    n = len(t) - m
    return [b_basis(x, t, m, j) for j in range(n-1)]

def differentiate(B):
    dB = np.zeros(np.shape(B))
    dB[1:,:] = B[1:,:] - B[:-1,:]
    dB[0,:] = dB[1,:]
    return dB

def lambda_selection(B, y, lambdas):
    B1 = differentiate(B)
    B2 = differentiate(B1)
    RSS = np.zeros(len(lambdas))
    dof = np.zeros(len(lambdas))
    for i in range(len(lambdas)):
        S = B @ np.linalg.inv(B.T @ B + lambdas[i] * (B2.T @ B2)) @ B.T
        yhat = S @ y
        RSS[i] = np.sum(np.power(yhat - y, 2))
        dof[i] = np.trace(S.dot(S))
    return RSS, dof

def generalized_cv(RSS, dof, n):
    return (RSS/n) / np.power((1 - (dof/n)), 2)

def plot_lambda_curves(B, y):
    fig = plt.figure(figsize=(16,4))
    ax = [plt.subplot(1, 3, i+1) for i in range(3)]
    n = 100
    min_lam, max_lam = 0, 1
    for i in range(len(ax)):
        lambdas = np.linspace(min_lam, max_lam, n)
        RSS, dof = lambda_selection(B, y, lambdas)
        GCV = generalized_cv(RSS, dof, n)
        lamhat = lambdas[np.argmin(GCV)]
        ax[i].ticklabel_format(style='sci', axis='y', scilimits=(0,0))
        ax[i].plot(lambdas, GCV)
        ax[i].scatter(lamhat, GCV[np.argmin(GCV)], c='r', s=20,
                      label=r'$\hat{\lambda}$')
        if i == 0:
            ax[i].set_ylabel(r'GCV($\lambda$)')
        ax[i].set_xlabel(r'$\lambda$')
        ax[i].axvline(lamhat, c='k', ls='--', lw=1, alpha=0.8)
        ax[i].text(lamhat + (0.015 * np.power(10, -i)),
                   np.max(GCV) - (np.max(GCV) - np.min(GCV)) * 0.05,
                   '$\hat{\lambda}$')
        min_lam = lamhat - (0.05 * np.power(10, -i))
        max_lam = lamhat + (0.05 * np.power(10, -i))
    return lamhat

def gaussian_kernel(u, d=1):
    return np.power(2 * np.pi, -d/2) * np.exp(-np.power(u, 2))

def loocv_gaus_kernel_regression(x, y, lambdas):
    m, n = len(lambdas), len(x)
    mse = np.zeros(m)
    for j in range(m):
        Lambda = lambdas[j]
        err = np.zeros(n)
        for i in range(n):
            x_ = np.delete(x, i)
            y_ = np.delete(y, i)
            z = gaussian_kernel((x[i] - x_) / Lambda)
            yke = np.sum(z * y_) / np.sum(z)
            err[i] = y[i] - yke
        mse[j] = np.mean(np.power(err, 2))
    return mse

# additional splines if needed


import numpy as np

def sigmoid(z: np.ndarray) -> np.ndarray:
    """The sigmoid function."""
    return 1.0 / (1.0 + np.exp(-z))

def sigmoid_prime(z: np.ndarray) -> np.ndarray:
    """Derivative of the sigmoid function."""
    y = sigmoid(z)
    return y * (1 - y)

def unitVector(i: int) -> np.ndarray:
    """
    Return a (10,1) ndarray with all elements as ``0.0``, except in the ``i``th index where the value is ``1.0``.
    """
    vector = np.zeros((10, 1))
    vector[i] = 1.0
    return vector


#! /usr/bin/env python

"""
File: sin_Taylor_series_diffeq.py
Copyright (c) 2016 Chinmai Raman
License: MIT
Course: PHYS227
Assignment: A.14
Date: Feb 25, 2016
Email: raman105@mail.chapman.edu
Name: Chinmai Raman
Description: Implements difference equations for computing a Taylor polynomial approximation to sin(x)
"""

import numpy as np

def sin_Taylor(x, N):
    n = 1
    an_prev = x
    sn_prev = 0
    while n <= N + 1:
        sn = sn_prev + an_prev
        an = - x**2 / ((2 * n + 1) * 2 * n) * an_prev
        sn_prev = sn
        an_prev = an
        n += 1
    return sn, abs(an)

def test_Taylor():
    assert(abs(sin_Taylor(np.pi / 2.0, 20)[0] - 1.0) < 1e-6), 'Failure'

def test_partc():
    assert(abs(sin_Taylor(np.pi / 2.0, 2)[0] - 1.0) < 1e-2), 'Failure'


from collections import namedtuple

from numpy import ones, zeros, eye, sqrt, tile, r_, array
from scipy.linalg import block_diag
from scipy.stats import chi2, t

from MinRelEntFP import MinRelEntFP
from Tscenarios import Tscenarios
from NormalScenarios import NormalScenarios

def PanicTDistribution(varrho2, r, c, nu, j_):
    # This function generates the joint scenarios and Flexible Probabilities of
    # a Panic t Distribution
    #  INPUTS
    #   varrho2      : [matrix] (n_ x n_) calm market correlation matrix
    #   r            : [scalar] homogeneous panic correlation
    #   c            : [scalar] prob threshold of a high-correlation crash event
    #   nu           : [scalar] degree of freedom
    #   j_           : [scalar] number of scenarios
    #  OPS
    #   X            : [matrix] (n_ x j_) joint scenarios
    #   p_           : [vector] (1 x j_) Flexible Probabilities (posterior via Entropy Pooling)
    #
    #NOTE: Moment matching of t-simulations for nu > 2

    # For details on the exercise, see here .

    ## Code
    n_ = len(varrho2)
    corr_c = varrho2
    corr_p = (1-r)*eye(n_) + r*ones((n_, n_)) # panic corr
    optionT = namedtuple('option', ['dim_red','stoc_rep'])
    optionT.dim_red = 0
    optionT.stoc_rep = 0
    if nu > 1:
        # Calm Component
        Xt_c = Tscenarios(nu, zeros((n_, 1)), corr_c, j_, optionT, 'Riccati')

        # Panic Component
        Xt_p = Tscenarios(nu, zeros((n_, 1)), corr_p, j_, optionT, 'Riccati')
    else:
        s2 = block_diag(corr_c, corr_p)
        Z = NormalScenarios(zeros((2*n_, 1)), s2, j_, 'Riccati')

        # Calm Component
        X_c = Z[:n_,:]
        Chi_2 = chi2.rvs(nu, 1, j_)
        Xt_c = X_c / tile(sqrt(Chi_2 / nu), (n_, 1))

        # Panic Component
        X_p = Z[n_:-1, :]
        Chi_2 = chi2.rvs(nu, 1, j_)
        Xt_p = X_p /tile(sqrt(Chi_2 / nu), (n_, 1))

    # Panic distribution
    B = (Xt_p < t.ppf(c, nu)) # triggers panic
    X = (1-B) * Xt_c + B * Xt_p

    # Perturb probabilities via Fully Flexible Views
    p = ones((1, j_)) / j_ # flat flexible probabilities (prior)
    aeq = r_[ones((1, j_)), X]   # constrain the first moments
    beq = r_[array([[1]]), zeros((n_, 1))]

    p_ = MinRelEntFP(p, None, None, aeq , beq)[0] # compute posterior probabilities
    return X, p_


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

from scipy.spatial.distance import pdist, cdist, squareform

from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from sklearn.metrics import adjusted_rand_score

# For reproducibility
np.random.seed(1000)

nb_samples = 1000
nb_clusters = 8

metric = 'minkowski'
p = 7
tolerance = 0.001

if __name__ == '__main__':
    # Create the dataset
    X, Y = make_blobs(n_samples=nb_samples, n_features=2, centers=nb_clusters,
                      cluster_std=1.2, center_box=[-5.0, 5.0], random_state=1000)

    # Perform K-Means clustering
    km = KMeans(n_clusters=nb_clusters, random_state=1000)
    C_km = km.fit_predict(X)

    print('Adjusted Rand score K-Means: {}'.format(adjusted_rand_score(Y, C_km)))

    # Perform K-Medoids clustering
    C = np.random.randint(0, nb_clusters, size=(X.shape[0],), dtype=np.int32)
    mu_idxs = np.zeros(shape=(nb_clusters, X.shape[1]))

    mu_copy = np.ones_like(mu_idxs)

    while np.linalg.norm(mu_idxs - mu_copy) > tolerance:
        for i in range(nb_clusters):
            Di = squareform(pdist(X[C == i], metric=metric, p=p))
            SDi = np.sum(Di, axis=1)

            mu_copy[i] = mu_idxs[i].copy()
            idx = np.argmin(SDi)
            mu_idxs[i] = X[C == i][idx].copy()

        C = np.argmin(cdist(X, mu_idxs, metric=metric, p=p), axis=1)

    print('Adjusted Rand score K-Medoids: {}'.format(adjusted_rand_score(Y, C)))

    # Show the final results
    sns.set()

    fig, ax = plt.subplots(1, 3, figsize=(26, 8), sharey=True)

    for i in range(nb_clusters):
        ax[0].scatter(X[Y == i, 0], X[Y == i, 1], label='Blob {}'.format(i + 1))
        ax[1].scatter(X[C_km == i, 0], X[C_km == i, 1], label='Cluster {}'.format(i + 1))
        ax[2].scatter(X[C == i, 0], X[C == i, 1], label='Cluster {}'.format(i + 1))

    ax[0].set_xlabel(r'$x_0$', fontsize=22)
    ax[1].set_xlabel(r'$x_0$', fontsize=22)
    ax[2].set_xlabel(r'$x_0$', fontsize=22)
    ax[0].set_ylabel(r'$x_1$', fontsize=22)

    ax[0].set_title('Ground truth', fontsize=18)
    ax[1].set_title('K-Means', fontsize=18)
    ax[2].set_title('K-Medoids with Minkowski metric and p=7', fontsize=18)

    ax[0].legend()
    ax[1].legend()
    ax[2].legend()

    plt.show()


import numpy as np
from PIL import Image
from copy import deepcopy
from matplotlib import pyplot as plt

path = 'lena_color.tiff'
image_tiff = Image.open(path)
rgb = np.array(image_tiff)

def RGB2YUV( rgb ): #RGB 2 YUV

    m = np.array([[ 0.299     ,  0.587     ,  0.114      ],
                  [-0.14714119, -0.28886916,  0.43601035 ],
                  [ 0.61497538, -0.51496512, -0.10001026 ]])

    yuv = np.dot(rgb, m.T)
    return yuv
yuv = RGB2YUV( rgb )

Y = yuv[:, :, 0] #Y - Channel
#%% Split Image to 8x8 Blocks

height, width = Y.shape
sliced = [] # new list for 8x8 sliced image
currY = 0 #current Y index
n = 8 #dividing 8x8 blocks
for i in range(n, height+1, n):
    currX = 0 #current X index
    for j in range(n, width+1, n):
        sliced.append(Y[currY:i,currX:j]-np.ones((8,8))*128) #Normalizing 128 from all pixels
        currX = j
    currY = i

T = [np.float32(Y) for Y in sliced]

#%% DCT algorithm

D = deepcopy(sliced)
csum = 0.0
for w in range(4096):
    for i in range(8):
        for j in range(8):
            for x in range(8):
                for y in range(8): #Main algorithm below
                    csum += T[w][x][y]*np.cos((2*x+1)*i*np.pi/(2*n))*np.cos((2*y+1)*j*np.pi/(2*n))
            if (i==0 and j==0): k = 0.5
            elif ((i==0 and j) or (i and j==0)): k = 1/np.sqrt(2)
            else: k = 1
            D[w][i][j] = 2*csum*k/n
            csum = 0

#%% Quantization Matrix

xQuanmatrix = np.array([[16,11,10,16, 24, 40, 51, 61],
                       [12,12,14,19, 26, 58, 60, 55],
                       [14,13,16,24, 40, 57, 69,56 ],
                       [14,17,22,29 ,51, 87, 80, 62],
                       [18,22,37,56, 68,109,103, 77],
                       [24,35,55,64,81, 104,113, 92],
                       [49,64,78,87,103,121,120,101],
                       [72,92,95,98,112,100,103, 99]])

#%% Quantization

C = D / xQuanmatrix
for i in range(4096):
    for j in range(8):
        for x in range(8):
            C[i][j][x] = int(round(C[i][j][x]))

#%% Inverse Quantization & IDCT algorithm

R = C * xQuanmatrix
N = deepcopy(sliced)

csum = 0;
for w in range(4096):
    for i in range(8):
        for j in range(8):
            for x in range(8):
                for y in range(8):
                    k = 0.5
                    csum += k*R[w][x][y]*np.cos((2*i+1)*x*np.pi/(2*n))*np.cos((2*j+1)*y*np.pi/(2*n))
            N[w][i][j] = csum*2/n
            csum = 0

#%% ReBuilding the Image

row = 0
rowNcol = []
for j in range(int(width/n), len(N)+1, int(width/n)):
    N[row:j] = N[row:j]+np.ones((8,8))*128
    rowNcol.append(np.hstack((N[row:j])))
    row = j
YC = np.vstack((rowNcol))

#%% Plotting

output = [Y,YC]
titles = ['Original', 'Compressed']

for i in range(2):
    plt.figure(figsize=(5,5))
    plt.axis('off')
    plt.title(titles[i])
    if i == 0:
        plt.imshow(output[i], cmap = 'gray')
    else:
        plt.imshow(output[i], cmap = 'gray')
plt.show()

#%% Computing PSNR

def PSNR(img1, img2):
    mse = np.mean((img1 - img2) ** 2)
    if mse == 0:
        return 100
    PIXEL_MAX = 255.0
    return 20 * np.log10(PIXEL_MAX / np.sqrt(mse))

print('PSNR between two images is :',PSNR(Y,YC))


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np

def f(x, r):
    """
    Implement the right-hand-side of the differential equation

        x' = r * x - x / (1 + x)
    """
    return r * x - x / (1 + x**2)

def plot(x, y, r):
    """
    Plot the vector field for a given value of the parameter `r`.

    Parameters
    ----------
    x: np.array
        The x-values
    y: np.array
        The y-values obtained for a specifc value of `r`
    r: float
        The value of `r` for which the `y` values have been obtained from `x`.
    """
    plt.plot(x, np.zeros(len(y)), color='grey')
    plt.plot(x, y)
    plt.xlabel('$x$')
    plt.ylabel(r'$\dot{x}$')
    plt.title(r'$\dot{x} = $' + f'$f(x, r={r})$')
    plt.show()

if __name__ == "__main__":
    x = np.linspace(-3., 3., 99)

    # Create a plot of the vector field for different values of r
    for r in [-2, 0, 0.5, 1, 5]:
        y = f(x, r)
        plot(x, y, r)