用numpy / scipy中的现有矩阵块来块三对角矩阵

时间:2018-07-12 21:35:53

标签: numpy matrix scipy sparse-matrix linear-algebra

我有以下numpy数组。

Sub Get_IS1()
Dim x As Integer
x = 0  

execute:
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual


Dim ws As Worksheet
Set ws = Sheets("Summary")
ws.Activate

Dim qurl, symbol As String

ticker = ws.Range("C9").Value
Exchange = ws.Range("C8").Value

'Delete Prior Connections
For Each cn In ThisWorkbook.Connections
cn.Delete
Next cn

'Clear Prior Data
Sheets("COMP1").Activate
Sheets("COMP1").Cells.Clear

'URL
qurl = "http://financials.morningstar.com/ajax/ReportProcess4CSV.html?&t=" & Exchange & ":" & ticker & "&region=usa&culture=en-US&cur=&reportType=is&period=12&dataType=A&order=asc&columnYear=5&curYearPart=1st5year&rounding=3&view=raw&r=618279&denominatorView=raw&number=3"

'Get Data Via Text File
With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;" & qurl & "" _
    , Destination:=Sheets("COMP1").Range("B1"))
    .Name = _
    "Table 1"
    .FieldNames = True
    .PreserveFormatting = False
    .RefreshStyle = xlInsertDeleteCells
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 65001
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileCommaDelimiter = True
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    On Error GoTo ends
    .Refresh BackgroundQuery:=False
  End With

Set ticker = Nothing
Set Exchange = Nothing
Set qurl = Nothing
Set ws = Nothing

Get_BS1

Exit Sub

'Error Handle for Invalid Entry
ends:
x = x + 1

If x = 5 Then
MsgBox ("No response was recived from Morningstar. Either an invalid ticker was entered or no prior records exist for the chosen symbol.")
ws.Activate
ElseIf x < 5 Then
GoTo execute
End If

End Sub

假定方括号内的所有矩阵均为diag = np.array([D_1, D_2, D_3]) ldiag = np.array([L_1, L_2]) udiag = np.array([U_1, U_2]) 。我要创建以下矩阵。

2 x 2

上面的零是|[D_1 | U_1 | 0 | |L_1 | D_2 |U_2 | |0, | L_2, |D_3]| 零矩阵。显然,这是一个具有上块[U_1,U_2]和下块[L_1,L_2]的三对角块矩阵。所得的三对角矩阵的尺寸为2 x 2

这是一些希望的代码。

6 x 6

如何进行这项工作?是否还有其他方法可以使用from scipy.sparse import spdiags import numpy as np diag = np.random.rand(3, 2, 2) # create 3, 2x2 matrices for diagonal udiag = np.random.rand(2, 2, 2) # create 2, 2x2 matrices for upper diagonal ldiag = np.random.rand(2, 2, 2) # create 2, 2x2 matrices for lower diagonal data = np.array([diag, udiag, ldiag]) diags = np.array([0, 1, -1]) spdiags(data, diags, 6,6).toarray() diagonalupper-diagonal的现有矩阵块创建块三对角矩阵?

当涉及的矩阵是缩放器时,即当它们是lower-diagonal矩阵时,此方法有效。当然有办法将其扩展为块矩阵吗?关于1x1矩阵还有其他问题。但是他们都没有解决这个特殊情况。

有帮助吗?

0 个答案:

没有答案