Numpy Exercises

本文通过多个练习探讨了矩阵运算的基本概念和技术,包括矩阵加法、乘法、求解线性方程组、计算范数、奇异值分解、幂迭代法及最近邻查找等。此外还讨论了这些操作在不同条件下的应用效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Generate matrices A, with random Gaussian entries, B, a Toeplitz matrix, where A ∈Rn×m and B ∈Rm×m, for n = 200, m = 500.
Exercise 9.1: Matrix operations Calculate A + A, AA>,A>A and AB. Write a function that computes A(B−λI) for any λ. 

#9.1
def computes(A, B, arg):
	return np.dot(A, B - np.dot(arg, np.eye(np.size(B,0),np.size(B,1))))


A = np.random.normal(0, 1, (200, 500))
B = toeplitz(np.random.random(500))

AaddA = A + A
np.savetxt('AaddA.txt', AaddA)
AAT = np.dot(A, A.T)
np.savetxt('AAT.txt', AaddA)
ATA = np.dot(A.T, A)
np.savetxt('ATA.txt', AaddA)
AB = np.dot(A, B)
np.savetxt('AB.txt', AaddA)

print("Solving A(B−λI)")
arg = float(input('Input the λ:'))
exp = computes(A, B, arg)
np.savetxt('exp.txt', exp)

Exercise 9.2: Solving a linear system Generate a vector b with m entries and solve Bx = b.

#9.2
b = np.random.random(500)
x = np.linalg.solve(B, b)
print("Solving B * x = b")
print("Residual: ", np.linalg.norm((np.dot(B, x) - b), ord = 2))

Exercise 9.3: Norms Compute the Frobenius norm of A: kAkF and the infinity norm of B: kBk∞. Also find the largest and smallest singular values of B.

#9.3
print("Frobenius norm of A:", np.linalg.norm(A, ord = 'fro'))
print("infinity norm of B:", np.linalg.norm(B, ord = np.inf))
print("The smallest singular of B:", min(np.linalg.svd(B, True, False)))


Exercise 9.4: Power iteration Generate a matrix Z, n × n, with Gaussian entries, and use the power iteration to find the largest eigenvalue and corresponding eigenvector of Z. How many iterations are needed till convergence?

Optional: use the time.clock() method to compare computation time when varying n.

#9.4
import time
def power_iteration(A):
	u = np.random.rand(A.shape[1])
	num_simulations = 0;

	while True:
		old = u[:]
		v = np.dot(A, u)
		arg = max(v) if max(v) + min(v) < 0 else min(v)
		u = v / arg
		num_simulations += 1
		if np.linalg.norm(u - old, ord = np.inf) < 10 ** (-6) or num_simulations >= 10 ** 4:
			break
	return u, arg, num_simulations

n = 100
Z = np.random.normal(0, 1, (n, n))
t1 = time.clock()
v, arg, num_simulations = power_iteration(Z)
t2 = time.clock()
print("simulations:",num_simulations, "Clock:", t2-t1, "Lambda:", arg)

Exercise 9.5: Singular values Generate an n×n matrix, denoted by C, where each entry is 1 with probability p and 0 otherwise. Use the linear algebra library of Scipy to compute the singular values of C. What can you say about the relationship between n, p and the largest singular value?

#9.5
'''
计算后发现最大的奇异值接近n*p
'''
from scipy.linalg import svd

for p in range(100):
	n = 100
	p = p / 100
	C = np.random.binomial(1, p, (n,n))
	U, s, Vh = svd(C)
	print(max(s))

Exercise 9.6: Nearest neighbor Write a function that takes a value z and an array A and finds the element in A that is closest to z. The function should return the closest value, not index.
Hint: Use the built-in functionality of Numpy rather than writing code to find this value manually. In particular, use brackets and argmin.

#9.6
def closest(z, A):
	return A[np.fabs(A - z).argmin()]

z = 3
A = np.random.random(10) * 10
print (closest(z, A))

内容概要:本文详细探讨了机组组合优化模型的构建,旨在通过合理安排各类发电机组的启停计划和优化出力分配,实现电力系统在经济性和稳定性上的最佳平衡。文章首先介绍了电力系统的四大主要组件——传统火电机组、风电机组、光伏机组和储能系统的参数及运行特性。接着,围绕最小化系统总运行成本这一目标,设计了优化目标函数,并明确了包括功率平衡约束、机组出力上下限约束、风光发电功率约束、弃风弃光约束、爬坡速率约束、储能系统荷电状态约束、充放电功率约束和充放电互斥约束在内的多项约束条件。最后,文章列出了求解机组组合优化模型所需的关键变量,如传统机组的开停状态、机组出力、启停成本、风电光伏实际出力、弃风弃光比例及储能系统的充放电功率和荷电状态,以实现系统的经济调度和可再生能源的最大化利用。 适合人群:从事电力系统研究、规划和调度工作的工程师和技术人员,以及对电力系统优化感兴趣的科研人员。 使用场景及目标:①帮助电力系统工程师理解不同类型发电机组的特点及其对系统稳定性、经济性和环保性的影响;②为制定合理的电力系统调度策略提供理论依据和技术支持;③促进可再生能源的有效整合,提高电力系统的灵活性和可靠性。 其他说明:本文提供的模型和方法不仅适用于当前的电力系统,也可为未来含高比例可再生能源接入的电力系统提供参考。文中涉及的具体数学公式和参数设定为实际应用提供了详细的指导,有助于提升电力系统的运行效率和经济效益。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值