Skip to content

murillio4/inf3331-python-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Some tips for assignment 4

Some numpy magic

a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]], np.int32)

b = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]], np.int32)

print(a*b)

#Result:
[[ 1  4  9]
 [16 25 36]
 [49 64 81]]
a = np.array([[2, 3, 2],
              [3, 3, 3],
              [2, 3, 2]], np.float64)

b = a < 3

print(b.type)

#result:
[[ True False  True]
 [False False False]
 [ True False  True]]
a = np.array([[True,  False,  True],
              [False, False,  False],
              [True,  False,  True]], np.bool)

b = np.array([[2, 2, 2],
              [2, 2, 2],
              [2, 2, 2]], np.float64)
b[a] += 2

print(b)

#result:
[[4. 2. 4.]
 [2. 2. 2.]
 [4. 2. 4.]]
a = np.array([[True,  False,  True],
              [False, False,  False],
              [True,  False,  True]], np.bool)

a = np.logical_not(a)

print(a)

#result:
[[False  True False]
 [ True  True  True]
 [False  True False]]
a = np.array([[2, 2, 2],
              [2, 2, 2],
              [2, 2, 2]], dtype=float)

b = np.full(c.shape, 1000, dtype=int)

print(b)

#result
[[1000 1000 1000]
 [1000 1000 1000]
 [1000 1000 1000]]

Numba

@jit
def square(x):
    return x ** 2
#return and parameter type defined
@jit(int32(int32))
def square(x):
    return x ** 2
#automatic parallelization
@jit(float32[:](float32[:], float32[:]), nopython=True, parallel=True)
def mul(arr1, arr2):
    return arr1*arr2

mul(np.random.rand(1000,1000), np.random.rand(3,2))

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published