潮流计算程序————稀疏技术(2)

时间:2021-2-20 作者:admin

按行(列)存储格式

潮流计算程序————稀疏技术(2)
潮流计算程序————稀疏技术(2)

import numpy as np
import random
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# matplotlib画图中中文显示会有问题,需要这两行设置默认字体


def sanju(A):
    VA = []
    IA = []
    JA = []
    for i in range(A.shape[0]):
        for j in range(A.shape[1]):
            if A[i, j] != 0:
                VA.append(i)
                IA.append(j)
                JA.append(A[i, j])
    print(VA, '\n', IA, '\n', JA, '\n')
    return VA, IA, JA


def anrows(A):
    VA = []
    JA = []
    IA = [0 for i in range(A.shape[0])]
    count = 0
    for i in range(A.shape[0]):
        cs = count
        f2 = 0
        count = 0
        for j in range(A.shape[1]):
            if A[i, j] != 0:
                count = count + 1
                VA.append(A[i, j])
                JA.append(j)
                f2 = 1  # 检测是否有非零元素,如果有,1,没有,0;
        if i > 0 and IA[i - 1] > 0:
            IA[i] = IA[i - 1] + cs
        elif i > 0 and IA[i - 1] == 0 and f2 == 1:
            IA[i] = 1
        elif f2 == 1 and i == 0:
            IA[i] = 1
        elif f2 == 0 and i == 0:
            IA[i] = 0
    c = [i for i in range(A.shape[0])]
    for i in range(A.shape[0]-1):
        for j in range(IA[i], IA[i + 1]):
            if IA[i] > 0:
                print(f'({i}, {JA[j-1]}):{VA[j - 1]} ', end='  ')
        print('')
    i = i + 1
    for j in range(IA[i], len(VA)+1):
        if IA[i] > 0:
            print(f'({i}, {JA[j-1]}):{VA[j - 1]} ', end='  ')
    print('')
    return VA, JA, IA


A = np.mat(np.zeros((10, 10)))
for i in range(100):
    a = random.randint(0, 9)
    b = random.randint(0, 9)
    A[a, b] = a*b
VAa, JAa, IAa = anrows(A)
print(A)

潮流计算程序————稀疏技术(2)

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。