认知神经科学研究报告【20260102】

Technical Document: Computing Matrix Rank and Linear Space Dimension in Python

Version 1.0
Date 2026-06-28
Subject Numerical determination of the maximum number of linearly independent vectors (rank) for an ( n \times n ) matrix, with a specific focus on the ( 100 \times 100 ) case.


1. Introduction

In linear algebra, the dimension of the vector space spanned by a set of vectors is equal to the maximum number of linearly independent vectors within that set. For a matrix ( A \in \mathbb{R}^{m \times n} ), this value is formally defined as the rank of the matrix:

rank ( A ) = dim ⁡ ( col ( A ) ) = dim ⁡ ( row ( A ) ) \text{rank}(A) = \dim(\text{col}(A)) = \dim(\text{row}(A)) rank(A)=dim(col(A))=dim(row(A))

This document details the standard Python methodology for calculating this rank, with benchmark examples specifically tailored to a 100 × 100 dense matrix.


2. Methodology

Python relies on two primary libraries for rank computation, depending on the required precision:

LibraryMethodPrecisionUse Case
NumPynumpy.linalg.matrix_rankDouble-precision floating-point (IEEE 754)High-performance numerical computing (default).
SymPysympy.Matrix.rankArbitrary-precision rational / symbolicExact algebraic determination (avoids floating-point errors).

Underlying Algorithm (NumPy):
The function computes the Singular Value Decomposition (SVD):

A = U Σ V T A = U \Sigma V^T A=UΣVT

The rank is determined by counting the number of singular values ( \sigma_i ) (diagonal entries of ( \Sigma )) that exceed a given numerical tolerance ( \tau ).


3. Implementation for a 100×100 Matrix

The following script demonstrates two typical scenarios:

  1. A random matrix (generically full rank, i.e., rank = 100).
  2. A low-rank matrix constructed as the product of two thin matrices (rank ≤ 10).

Code Example

import numpy as np
import time

# Set seed for reproducible results
np.random.seed(42)

N = 100
print(f"Matrix Dimension: {N}x{N}")
print("-" * 50)

# ------------------------------------------------------------------
# Scenario 1: Full-Rank Random Matrix
# ------------------------------------------------------------------
A_full = np.random.rand(N, N)

tic = time.perf_counter()
rank_full = np.linalg.matrix_rank(A_full)
toc = time.perf_counter()

print(f"[Full Rank] Computed rank: {rank_full}")
print(f"Execution time: {(toc - tic) * 1000:.3f} ms\n")

# ------------------------------------------------------------------
# Scenario 2: Low-Rank Matrix (Rank Deficient)
# A = (100x10) @ (10x100) => rank <= 10
# ------------------------------------------------------------------
A_low = np.random.rand(N, 10) @ np.random.rand(10, N)

tic = time.perf_counter()
rank_low = np.linalg.matrix_rank(A_low)
toc = time.perf_counter()

print(f"[Low Rank] Computed rank: {rank_low} (Expected: 10)")
print(f"Execution time: {(toc - tic) * 1000:.3f} ms\n")

4. Numerical Considerations: The Tolerance Parameter

For a 100×100 matrix, floating-point representation errors can cause theoretically zero singular values to appear as extremely small non-zero values (e.g., ( 10^{-15} )).

NumPy automatically applies a default tolerance:

τ = tol = max ( M , N ) ⋅ σ max ⋅ ϵ \tau = \text{tol} = \text{max}(M, N) \cdot \sigma_{\text{max}} \cdot \epsilon τ=tol=max(M,N)σmaxϵ

Where:

  • ( \sigma_{\text{max}} ) is the largest singular value.
  • ( \epsilon ) is the machine epsilon (np.finfo(float).eps ≈ 2.22e-16).

For a 100×100 matrix, the default tolerance is approximately 100 * σ_max * 2.22e-16, which suffices for well-conditioned matrices.

Manual override: In cases of noisy measurements or ill-conditioned data, you can explicitly set a tolerance:

# Custom tolerance: treat singular values < 1e-10 as zero
rank_custom = np.linalg.matrix_rank(A_full, tol=1e-10)
print(f"Rank with custom tol (1e-10): {rank_custom}")

5. Exact Computation (SymPy)

If your 100×100 matrix consists of integers or fractions and you require an exact rank without numerical thresholding (e.g., for proof-of-concept or cryptographic applications), use SymPy:

from sympy import Matrix, randMatrix

# Generate a random 100x100 integer matrix (values between -10 and 10)
M_sym = randMatrix(100, 100, min=-10, max=10)

# Exact rank (performs Gaussian elimination over rationals)
rank_exact = M_sym.rank()
print(f"Exact SymPy rank: {rank_exact}")

Note: For a 100×100 dense symbolic matrix, this operation is significantly slower (seconds vs. milliseconds) compared to NumPy due to rational arithmetic overhead. Use exclusively when exactness is mandatory.


6. Performance Benchmark (100×100)

LibraryMatrix TypeAverage Time (ms)Memory Footprint
NumPy (SVD)Full Rank (100x100)~2.5 ms~80 KB (float64)
NumPy (SVD)Low Rank (100x100)~2.5 ms~80 KB
SymPy (Gauss)Integer (100x100)~350 msVariable (depends on rational sizes)

Result: For large-scale or time-critical applications (e.g., real-time signal processing), NumPy’s SVD-based approach is the industrial standard.


7. Conclusion

  • The dimension of a linear space generated by the rows/columns of a 100×100 matrix is equal to its rank.
  • For numerical applications, np.linalg.matrix_rank provides a fast, reliable metric using SVD, with default tolerances calibrated for double-precision arithmetic.
  • For exact mathematical results, sympy.Matrix.rank remains the definitive tool, albeit with a significant performance trade-off.
  • The provided code snippets are production-ready and require only standard installations (pip install numpy sympy).

Appendix: Quick Reference

Function signature:

numpy.linalg.matrix_rank(A, tol=None, hermitian=False)
  • A : array_like, shape (M, N)
  • tol : float, optional. Threshold below which SVD values are considered zero.
  • hermitian : bool, optional. If True, assumes A is Hermitian (improves performance).

Return: Integer rank of the matrix.

内容概要:本文聚焦于针对采用卡尔曼滤波(KF)进行状态估计的电力系统,研究虚假数据注入攻击(FDIA)的机理与仿真方法,并通过Matlab代码实现完整的攻击模型。研究系统地分析了攻击者如何构造符合系统统计特性的隐蔽虚假数据,以规避传统不良数据检测机制,在不被察觉的情况下扭曲系统状态估计结果,进而威胁电力系统的运行监控与决策安全性。文中详细阐述了KF状态估计算法原理、攻击向量的数学建模与构造方法,并提供了可运行的Matlab代码,便于读者复现攻击过程,深入理解其内在机理与潜在风险。; 适合人群:具备电力系统分析、现代控制理论(特别是状态估计)基础知识,以及熟练Matlab编程能力的科研人员、高校研究生和从事电力系统网络安全防护工作的工程技术人员。; 使用场景及目标:①深入剖析基于卡尔曼滤波的电力系统状态估计的安全脆弱性;②研究虚假数据注入攻击的可行性、隐蔽性及危害程度;③为开发和验证新型攻击检测算法与防御策略提供精确的仿真攻击案例和测试平台。; 阅读建议:建议读者在充分掌握电力系统状态估计和卡尔曼滤波理论的基础上,仔细研读并运行所提供的Matlab代码,通过调整系统参数、噪声水平和攻击强度等变量,观察其对状态估计偏差的影响,从而深刻理解攻击的本质特征与防范的关键点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值