mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-02-03 02:24:03 +08:00
42 lines
716 B
Python
42 lines
716 B
Python
|
|
###############
|
|
# Authored by Weisheng Jiang
|
|
# Book 4 | From Basic Arithmetic to Machine Learning
|
|
# Published and copyrighted by Tsinghua University Press
|
|
# Beijing, China, 2022
|
|
###############
|
|
|
|
# Bk4_Ch4_05.py
|
|
|
|
import numpy as np
|
|
|
|
# define matrix
|
|
A = np.matrix([[1, 2],
|
|
[3, 4],
|
|
[5, 6]])
|
|
|
|
# scaler
|
|
k = 2;
|
|
|
|
# column vector c
|
|
c = np.array([[3],
|
|
[2],
|
|
[1]])
|
|
|
|
# row vector r
|
|
r = np.array([[2,1]])
|
|
|
|
# broadcasting principles
|
|
|
|
# matrix A plus scalar k
|
|
A_plus_k = A + k
|
|
|
|
# matrix A plus column vector c
|
|
A_plus_a = A + c
|
|
|
|
# matrix A plus row vector r
|
|
A_plus_r = A + r
|
|
|
|
# column vector c plus row vector r
|
|
c_plus_r = c + r
|