spdiags()在MATLAB中转换为Python [英] spdiags() in MATLAB into Python

查看:1106
本文介绍了spdiags()在MATLAB中转换为Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将MATLAB实现转换为Python 3实现。我发现了一个函数,spdiags(),我不明白,也不知道如何将它翻译成Python 3。



功能在这里:
http://www.mathworks.com/help /matlab/ref/spdiags.html

关于一个名字相同的函数的Scipy文档在这里:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse .spdiags.html



MATLAB函数的功能是什么,并且是否有一个Python实现具有相同的返回值?

解决方案

在Octave(MATLAB替代版)中,文档中的示例如下:

 八度:7个x = spdiags(重塑(1:12,4,3),[-1 0 1],5,4); 
八度:8>全(x)#显示为全矩阵或密集矩阵
ans =
5 10 0 0
1 6 11 0
0 2 7 12
0 0 3 8
0 0 0 4

存储在 x 是:

  x = 
压缩的列稀疏(rows = 5,cols = 4,nnz = 11 [55%])
(1,1) - > 5
(2,1) - > 1
(1,2) - > 10
(2,2) - > 6
(3,2) - > 2
(2,3) - > 11
(3,3) - > 7
(4,3) - > 3
(3,4) - > 12
(4,4) - > 8
(5,4) - > 4

相当于 scipy.sparse 表达式:

 在[294]中:x = sparse.spdiags(np.arange(1,13).reshape(3,4) ,[-1,0,1],5,4)
在[295]中:xA#显示为正常的numpy数组
Out [295]:
array([[5,10 ,0,0],
[1,6,11,0],
[0,2,7,12],
[0,0,3,8],

$ b在[296]中:x
Out [296]:
<5x4类型'稀疏矩阵'< class'numpy.int32'>'
,其中11个存储元素(3个对角线)采用DIAgonal格式>

它使用 dia 格式,但它使用 x.tocsc()轻松转换为 csc (相当于Octave格式)。



为了查看相同的坐标和值,我们可以使用 dok 格式(一个字典子类):

 在[299]中:dict(x.todok())
输出[299]:
{(0,1): 10,
(1,2):11,
(3,2):3,
(0,0):5,
(3,3):8, (2,1):2,
(2,3):12,
(4,3):4,
(2,2):7,
(1,0):1,
(1,1):6}

相同的值,针对基于0的索引进行调整。



在这两种情况下,对角线值都来自矩阵:

 八度:10 -10重塑(1:12,4,3)
ans =
1 5 9
2 6 10
3 7 11
4 8 12

In [302]:np.arange(1,13).reshape(3,4)
Out [302]:
数组([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]])

Octave / MATLAB按行排列值, numpy ,因此不同的重塑 numpy 矩阵是相当于MATLAB的转置。



请注意 9 已被省略(4项映射到3个元素对角线)。



另一个参数是一个列表的对角线设置, [ - 1,0,1] 和最终形状(5,4)



大部分参数差异必须在MATLAB和numpy之间做基本的区别。另一个区别是MATLAB只有一个稀疏矩阵表示,scipy有六个。


I am trying to translate a MATLAB implementation into a Python 3 implementation. I've found a function, spdiags(), that I do not understand, and am also not sure how to translate it into Python 3.

The MATLAB documentation on the function is here: http://www.mathworks.com/help/matlab/ref/spdiags.html

The Scipy documentation on an identically named function is here: http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.spdiags.html

What does the MATLAB function do, and is there a Python implementation of the same return available?

解决方案

In Octave (MATLAB alternative), the example in its documentation:

octave:7> x = spdiags (reshape (1:12, 4, 3), [-1 0 1], 5, 4);
octave:8> full(x)  # display as a full or dense matrix
ans =    
    5   10    0    0
    1    6   11    0
    0    2    7   12
    0    0    3    8
    0    0    0    4

The actual values that are stored in x are:

x =
Compressed Column Sparse (rows = 5, cols = 4, nnz = 11 [55%])
  (1, 1) ->  5
  (2, 1) ->  1
  (1, 2) ->  10
  (2, 2) ->  6
  (3, 2) ->  2
  (2, 3) ->  11
  (3, 3) ->  7
  (4, 3) ->  3
  (3, 4) ->  12
  (4, 4) ->  8
  (5, 4) ->  4

The equivalent scipy.sparse expression:

In [294]: x = sparse.spdiags(np.arange(1,13).reshape(3,4), [-1, 0, 1], 5, 4)
In [295]: x.A   # display as normal numpy array
Out[295]: 
array([[ 5, 10,  0,  0],
       [ 1,  6, 11,  0],
       [ 0,  2,  7, 12],
       [ 0,  0,  3,  8],
       [ 0,  0,  0,  4]])

In [296]: x
Out[296]: 
<5x4 sparse matrix of type '<class 'numpy.int32'>'
    with 11 stored elements (3 diagonals) in DIAgonal format>

This use the dia format, but it easy to transfrom to csc (equivalent to the Octave format) with x.tocsc().

To see the same coordinates and values, we can use the dok format (a dictionary subclass):

In [299]: dict(x.todok())
Out[299]: 
{(0, 1): 10,
 (1, 2): 11,
 (3, 2): 3,
 (0, 0): 5,
 (3, 3): 8,
 (2, 1): 2,
 (2, 3): 12,
 (4, 3): 4,
 (2, 2): 7,
 (1, 0): 1,
 (1, 1): 6}

Same values, adjusting for the 0 based indexing.

In both cases, the diagonal values come from a matrix:

octave:10> reshape(1:12, 4, 3)
ans =
    1    5    9
    2    6   10
    3    7   11
    4    8   12

In [302]: np.arange(1,13).reshape(3,4)
Out[302]: 
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

Octave/MATLAB arrange values by column, numpy by row, hence the different reshape. The numpy matrix is the transpose of the MATLAB equivalent.

Note that 9 has been omitted from both (4 items mapped on to a 3 element diagonal).

The other argument is a list of diagonals to set, [-1,0,1], and final shape (5,4).

Most of the differences in arguments have to do the basic differences between MATLAB and numpy. The other difference is that MATLAB has only one sparse matrix representation, scipy has a half dozen.

这篇关于spdiags()在MATLAB中转换为Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆