Skip to content

Commit

Permalink
added back misc
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsjoberg committed Oct 28, 2023
1 parent 3682299 commit 875c62b
Show file tree
Hide file tree
Showing 26 changed files with 1,389 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Python/Misc/_alu_tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
| x | y |zx|nx|zy|ny|f|no| out |zr|ng|
|0000000000000000|1111111111111111| 1| 0| 1| 0|1| 0|0000000000000000| 1| 0|
|0000000000000000|1111111111111111| 1| 1| 1| 1|1| 1|0000000000000001| 0| 0|
|0000000000000000|1111111111111111| 1| 1| 1| 0|1| 0|1111111111111111| 0| 1|
|0000000000000000|1111111111111111| 0| 0| 1| 1|0| 0|0000000000000000| 1| 0|
|0000000000000000|1111111111111111| 1| 1| 0| 0|0| 0|1111111111111111| 0| 1|
|0000000000000000|1111111111111111| 0| 0| 1| 1|0| 1|1111111111111111| 0| 1|
|0000000000000000|1111111111111111| 1| 1| 0| 0|0| 1|0000000000000000| 1| 0|
|0000000000000000|1111111111111111| 0| 0| 1| 1|1| 1|0000000000000000| 1| 0|
|0000000000000000|1111111111111111| 1| 1| 0| 0|1| 1|0000000000000001| 0| 0|
|0000000000000000|1111111111111111| 0| 1| 1| 1|1| 1|0000000000000001| 0| 0|
|0000000000000000|1111111111111111| 1| 1| 0| 1|1| 1|0000000000000000| 1| 0|
|0000000000000000|1111111111111111| 0| 0| 1| 1|1| 0|1111111111111111| 0| 1|
|0000000000000000|1111111111111111| 1| 1| 0| 0|1| 0|1111111111111110| 0| 1|
|0000000000000000|1111111111111111| 0| 0| 0| 0|1| 0|1111111111111111| 0| 1|
|0000000000000000|1111111111111111| 0| 1| 0| 0|1| 1|0000000000000001| 0| 0|
|0000000000000000|1111111111111111| 0| 0| 0| 1|1| 1|1111111111111111| 0| 1|
|0000000000000000|1111111111111111| 0| 0| 0| 0|0| 0|0000000000000000| 1| 0|
|0000000000000000|1111111111111111| 0| 1| 0| 1|0| 1|1111111111111111| 0| 1|
|0000000000010001|0000000000000011| 1| 0| 1| 0|1| 0|0000000000000000| 1| 0|
|0000000000010001|0000000000000011| 1| 1| 1| 1|1| 1|0000000000000001| 0| 0|
|0000000000010001|0000000000000011| 1| 1| 1| 0|1| 0|1111111111111111| 0| 1|
|0000000000010001|0000000000000011| 0| 0| 1| 1|0| 0|0000000000010001| 0| 0|
|0000000000010001|0000000000000011| 1| 1| 0| 0|0| 0|0000000000000011| 0| 0|
|0000000000010001|0000000000000011| 0| 0| 1| 1|0| 1|1111111111101110| 0| 1|
|0000000000010001|0000000000000011| 1| 1| 0| 0|0| 1|1111111111111100| 0| 1|
|0000000000010001|0000000000000011| 0| 0| 1| 1|1| 1|1111111111101111| 0| 1|
|0000000000010001|0000000000000011| 1| 1| 0| 0|1| 1|1111111111111101| 0| 1|
|0000000000010001|0000000000000011| 0| 1| 1| 1|1| 1|0000000000010010| 0| 0|
|0000000000010001|0000000000000011| 1| 1| 0| 1|1| 1|0000000000000100| 0| 0|
|0000000000010001|0000000000000011| 0| 0| 1| 1|1| 0|0000000000010000| 0| 0|
|0000000000010001|0000000000000011| 1| 1| 0| 0|1| 0|0000000000000010| 0| 0|
|0000000000010001|0000000000000011| 0| 0| 0| 0|1| 0|0000000000010100| 0| 0|
|0000000000010001|0000000000000011| 0| 1| 0| 0|1| 1|0000000000001110| 0| 0|
|0000000000010001|0000000000000011| 0| 0| 0| 1|1| 1|1111111111110010| 0| 1|
|0000000000010001|0000000000000011| 0| 0| 0| 0|0| 0|0000000000000001| 0| 0|
|0000000000010001|0000000000000011| 0| 1| 0| 1|0| 1|0000000000010011| 0| 0|
Binary file added Python/Misc/_images/edges.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Python/Misc/_images/photo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
222 changes: 222 additions & 0 deletions Python/Misc/_numpy-primer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import numpy as np

## basics

a = [60, 28, 49, 81]
b = [55, 634, 704, 2020]

# create numpy array
A = np.array(a, dtype='i')
# print(A)
# [60 28 49 81]

AB = np.array([a, b])
# print(AB)
# [[ 60 28 49 81]
# [ 55 634 704 2020]]

print(AB.ndim)
# 2
print(AB.shape)
# (2, 4)

arr_zero = np.zeros(4)
# print(arr_zero)
# [0. 0. 0. 0.]

arr_ones = np.ones((2, 3))
# print(arr_ones)
# [[ 1. 1. 1.]
# [ 1. 1. 1.]]

arr_range = np.arange(0, 10)
# print(arr_range)
# [0 1 2 3 4 5 6 7 8 9]

arr_linspace = np.linspace(0, 1, 5)
# print(arr_linspace)
# [0. 0.25 0.5 0.75 1. ]

arr_log = np.logspace(0, 1, 5)
# print(arr_log)
# [ 1. 1.77827941 3.16227766 5.62341325 10. ]

# slicing
print(A[1:3])
# [28 49]
print(AB[0:2, 2:4])
# [[ 49 81]
# [ 704 2020]]

# reverse order
print(A[::-1])
# [81 49 28 60]

# operations
print(arr_range.mean())
# 4.5
print(arr_range.sum())
# 45
print(AB.min())
# 28
print(AB.max())
# 2020
print(np.sin(AB))
# [[-0.30481062 0.27090579 -0.95375265 -0.62988799]
# [-0.99975517 -0.56605794 0.27947339 0.04406199]]
print(np.multiply(a, b))
# [ 3300 17752 34496 163620]
print(np.sqrt(b))
# [ 7.41619849 25.17935662 26.53299832 44.94441011]

# min value in each column
print(np.minimum(a, b))
# [55 28 49 81]

# sum of axis (columns)
print(np.sum(AB, axis=1))
# [ 218 3413]

## indexing

# create array with 8 integers
A = 2 * np.arange(8)**2 + 1
# print(A)
# [ 1 3 9 19 33 51 73 99]

# create array with 16 integers
B = np.arange(16)
B.shape = (4, 4)
# print(B)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]
# [12 13 14 15]]

print(A[[3, -1, 1]])
# [19 99 3]
print(B[:, [2, -1, 0]])
# [[ 2 3 0]
# [ 6 7 4]
# [10 11 8]
# [14 15 12]]

# elements from A at indices given by fibonacci sequence
fibseq = np.array([0, 1, 1, 2, 3])
# print(A[fibseq])
# [ 1 3 3 9 19]

# diagonal
i = np.arange(4)
print(B[i, i])
# [ 0 5 10 15]

# diagonal of part
print(B[i[:3], i[:3] + 1])
# [ 1 6 11]

## masking

# create array with 16 integers
C = np.arange(16)
C.shape = (4, 4)
# print(C)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]
# [12 13 14 15]]

# create mask with True
mask = np.ones(4, dtype=bool)
# print(mask)
# [ True True True True]

# diagonal
print(C[mask, mask])
# [ 0 5 10 15]

# conditional mask
conditional = (C >= 9)
# print(C[conditional])
# [ 9 10 11 12 13 14 15]

# get all values smaller than 9
print(C[C < 9])
# [0 1 2 3 4 5 6 7 8]

# get all values smaller than 5 or larger than 10
print(C[(C < 5) | (C > 10)])
# [ 0 1 2 3 4 11 12 13 14 15]

# where
print(C[np.where(C > 10)])
# [11 12 13 14 15]

# get all columns with a value less than 3
print(C[:, np.where(C < 3)[1]])
# [[ 0 1 2]
# [ 4 5 6]
# [ 8 9 10]
# [12 13 14]]

## operations

A = np.arange(6)
print(A)
# [0 1 2 3 4 5]
print(A + A)
# [ 0 2 4 6 8 10]
print(A - 1)
# [-1 0 1 2 3 4]
print(A * 6)
# [ 0 6 12 18 24 30]

# gradient
X = np.array([2, 4, 5])
Y = np.array([6, 2, 8])
print(np.gradient(X))
# [ 2. 1.5 1. ] => (4 - 2) / 1, (5 - 2) / 2, (5 - 4)/ 1
print(np.gradient(Y))
# [-4. 1. 6.] => (2 - 6) / 1, (8 - 6) / 2, (8 - 2)/ 1

# numerical derivative
print(np.gradient(Y) / np.gradient(X))
# [-2. 0.66666667 6. ]

B = np.arange(4)
B.shape = (2, 2)
# print(B)
# [[0 1]
# [2 3]]

v = np.array([[2],[4]])
# print(v)
# [[2]
# [4]]

# scalar (multiplication)
print(B * v)
# [[ 0 2]
# [ 8 12]]

# dot product
print(np.dot(B, v))
# [[ 4]
# [16]]

# operations on higher dimensions
M = np.arange(12)
M.shape = (4, 3)
# print(M)
# [[ 0 1 2]
# [ 3 4 5]
# [ 6 7 8]
# [ 9 10 11]]

v = np.array([16, 17, 18])

print(M + v)
# [[16 18 20]
# [19 21 23]
# [22 24 26]
# [25 27 29]]
111 changes: 111 additions & 0 deletions Python/Misc/_pandas-series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import pandas as pd

# creating series (one-dimensional array)
simple_series = pd.Series([42, 55, 73], dtype='f8')
# 0 42.0
# 1 55.0
# 2 73.0
# dtype: float64

# creating series with specified index
index_series = pd.Series([42, 55, 73], index=["electron", "proton", "neutron"], dtype='f8')
# electron 42.0
# proton 55.0
# neutron 73.0
# dtype: float64

# accessing value in series
print(index_series["electron"])
# 42.0

# accessing multiple values in series (range)
print(index_series["electron":"neutron"])
# electron 42.0
# proton 55.0
# neutron 73.0
# dtype: float64

# accessing multiple values in series using indexing
print(index_series[1:])
# proton 55.0
# neutron 73.0
# dtype: float64

# creating series from dictionary
dict_series = pd.Series({ "electron": 6, "neutron": 28, "proton": 496, "neutrino": 8128 })
# electron 6
# neutrino 8128
# neutron 28
# proton 496
# dtype: int64

# combining two series into one column
print(index_series + dict_series)
# electron 48.0
# neutrino NaN
# neutron 101.0
# proton 551.0
# dtype: float64

# combining two series as dataframe
combined_series = pd.DataFrame({ "A": index_series, "B": dict_series })
# A B
# electron 42.0 6
# neutrino NaN 8128
# neutron 73.0 28
# proton 55.0 496

# accessing columns as series
print(combined_series["A"])
# electron 42.0
# neutrino NaN
# neutron 73.0
# proton 55.0
# Name: A, dtype: float64

# add new row with index
append_series = combined_series._append(pd.DataFrame({ "A": [-8128] }, index=["antineutrino"]))
# A B
# electron 42.0 6.0
# neutrino NaN 8128.0
# neutron 73.0 28.0
# proton 55.0 496.0
# antineutrino -8128.0 NaN

# drop row
append_series = append_series.drop("neutron")
# A B
# electron 42.0 6.0
# neutrino NaN 8128.0
# proton 55.0 496.0
# antineutrino -8128.0 NaN

# transpose
print(combined_series.T)
# electron neutrino neutron proton
# A 42.0 NaN 73.0 55.0
# B 6.0 8128.0 28.0 496.0

# masking
print(combined_series > 120)
# A B
# electron False False
# neutrino False True
# neutron False False
# proton False True

# add masking as column (e.g. larger than 120)
combined_series["large"] = (combined_series["A"] > 120) | (combined_series["B"] > 120)
# A B large
# electron 42.0 6 False
# neutrino NaN 8128 True
# neutron 73.0 28 False
# proton 55.0 496 True

# delete a column
del combined_series["large"]
# A B
# electron 42.0 6
# neutrino NaN 8128
# neutron 73.0 28
# proton 55.0 496
5 changes: 5 additions & 0 deletions Python/Misc/_square.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

int square(int num) {
return num * num;
}
Binary file added Python/Misc/_square.so
Binary file not shown.
Loading

0 comments on commit 875c62b

Please sign in to comment.