O.1. Numpy - JulTob/Python GitHub Wiki

import numpy


import math
import numpy as np


dir(numpy)


pi = np.pi
radian = 2 * pi
degree = 360
euler = np.e


math.sin(radian)
numpy.sin(radian)
numpy.sin(complex)

from numpy import sin, log, cos
sin(x)  # works

from numpy import *
#-- Functions:
sqrt(x)   square root of x
exp(x)    exponential of x     e^x
log(x)    natural log of x,    ln(x)
log10(x)  base 10 log of x

degrees(x) converts x from radians to degrees
radians(x) converts x from degrees to radians

sin(x)    sine of x (x in radians)
cos(x)    cosine x (x in radians)
tan(x)    tangent x (x in radians)
arcsin(x) Arc sine (in radians) of x
arccos(x) arc cosine (in radians) of x
arctan(x) arc tangent (in radians) of x

fabs(x)   absolute value of x
 
math.factorial(n) n! of an integer

round(x) rounds a float to nearest integer
floor(x) rounds a float down to nearest integer
ceil(x)  rounds a float up to nearest integer

sign(x) −1 if x < 0, +1 if x > 0, 0 if x = 0

# Matrizes 
zeros(n)  # square n-matrix of zeros
zeros(n,m) # nxm-matrix of zeros
zeros(d_size)  # Multidimensional Matrix of dimensions and size 
ones()    # Same as zeros but with ones
eye()     # Diagonal ones

A = np.array( [ [2, 3, 5], [7, 11, 13] ] )
A[0]     
  #  [2, 3, 5]
A[0][1]   
  #  3

a = np.ones(4)  
a = np.eye(3)  
NxM = a.shape
Size = a.size

column = np.zeros((N,1)) 
row = np.zeros((1,N)) 
np.size(a)

a.sum()
a.mean()
a.std()

Evenly Spaced

from = 2
to = 6
step = 0.5
n_steps = 100
count_from_zero = np.arange(to)
count_up = np.arange(from,to) #last might not be to
ruler = np.arange(from,to,step)
help(np.arange)
np.linspace(from,to,n_steps) # last is "to" in n_steps


x_min = 0
x_max = 10
dx = 0.1
x_array = np.arange(x_min, x_max + dx, dx)

Concatenation

np.hstack()
np.vstack()

a = np.zeros( (2, 3) )
b = np.ones( (2, 3) )
h = np.hstack( [a, b] )
v = np.vstack( [a, b] )

Slicing

N = np.size(A, 1)
x = A[0:N:1, 0]
y = A[0:N:1, 1]

x = A[:, 0]
y = A[:, 1]

Flatenning and reshapping


a = np.array( [ [1, 2], [2, 1] ] )
b = a.flatten()
c = np.ravel(a)
d = a.ravel()

1D NumPy Array

array(a)      
   # Create an array from yhe list `a`
linspace(start, stop, num)
   # Returns num evenly spaced numbers over an interval from start to stop inclusive. (num=50 if omitted)
logspace(start, stop, num)  
   # Returns num logarithmically spaced numbers over an interval from 10^start to 10^stop,inclusive. (num=50 if omitted)
arange([start,] stop[, step], dtype=None)
   #   Returns data points from start to end, exclusive, evenly spaced by step. (step=1 if omitted. start=0 and step=1 if both are omitted.)
zeros(num, dtype=float)
   #  Returns an array of 0s with num elements. Optional dtype argument can be used to set the data type; left unspecified, a float array is made.
ones(num, dtype=float)
   #  Returns an array of 1s with num elements. Optional dtype argument can be used to set the data type; left unspecified, a float array is made.

a = np.arange(12)
b = np.reshape(a, (3, 4) )
c = b.reshape( (2, 6) )
d = c.reshape( (2, 3, 2) )

Random

r = np.random.random(4)