Sparse Matrix's Transpose

原创 2016年05月31日 15:00:23

Sparse Matrix

Usually, suppose that there is m rows and n cols, and t non-zero element in a matrix. Make δ=t/(mn), δ is called sparse factor. If δ0.05, we could call the matrix a sparse matrix.

Transpose Algorithm

  • The data is ordered by row.

About classical method:

  • According to matrix’s diagonal, making half of number of all element( munu) tranpose is a easy way to realize matrix’s transpose.

    Time Complexity: O(munu)


There are two methods.
  • First

    Requirement:
    The result must orderly by col. If don’t require the result is orderly, We could do this in time O(nu).

    Description:
    From first row, find the elem whose row-num(i) is this row, and transpose to col.

    // M is source Matrix, T is target Matrix
    // mu is row-num, nu is col-num, tu is non-zero-num
    // .i is row, .j is col, .e is elem
    
    Status TranposeMatrix(Matrix M, Matrix &T) {
        T.mu = M.nu, T.nu = m.Mu, T.tu = M.tu;
        if (T.mu) {
            q = 1;
            for(col = 1; col <= M.nu; ++col)
            for(p = 1; p <= M.tu; ++p) 
                if(M.data[p].j == col) {
                    T.data[q].i = M.data[p].i;
                    T.data[q].j = M.data[p].j;
                    T.data[q].e = M.data[p].e;
                    ++q;
                }
        }
        return OK;
    }

    Obviously, there is nutu times operation.

    Time Complexity: O(nutu)

    If tumu, this method is bad.

  • second

    Name: Fast Transpose

    Description:
    If we could determine every col of result serial number, we could make every data right location.

    Known:
    cpot[1] = 1;
    cpot[col] = cpot[col- 1] + num[col- 1];

    Because the source data is ordered by row, we could make every col a point to mark this col’s the data sequence.

    Status FastTransposeMatrix(Matrix M, Matrix &T) {
        T.mu = M.nu; T.nu = M.mu; T.tu = M.tu;
        if(T.tu) {
            for(col = 1; col <= M.nu; ++col) num[col] = 0;
            for(t = 1; t <= M.tu; ++t) ++num[M.data[t].j];
            cpot[i] = 1;
    
            for (col = 2; col <= M.nu; ++col) cpot[col] = cpot[col - 1] + num [col - 1];
            for (p= 1; p<= M.tu; ++p) {
                col = M.data[p].j;
                q = cpot[col];
                T.data[q].i = M.data[p].j;
                T.data[q].j = M.data[p].i;
                T.data[q].e = M.data[p].e;
                ++ cpot[col];
            }
        }
    }

    Obviously, there is nu+tu times operation.

    Time Complexity: O(nu+tu)

    In worst situation, tu=munu, this time is O(munu), it still works well.

版权声明:如果文中有任何问题或者值得讨论的地方,都可以在下方留言或者QQ526664687,欢迎交流!!!

相关文章推荐

matrix transpose

  • 2014-08-11 20:15
  • 380B
  • 下载

【Leetcode Shell】Transpose File

题目: Given a text file file.txt, transpose its content. You may assume that each row has th...

【TensorFlow】tf.nn.conv2d_transpose是怎样实现反卷积的?

三个月没更新了啊,回来更一发~~ csdn上主要讲一些coding过程中遇到的函数,问题,解决方案。偏实践 另外,如果你想看一些理论方面的东西,欢迎加我的知乎 csdn私信几乎不看,有问题交流可以发邮...

Transpose File

Transpose File Total Accepted: 3876 Total Submissions: 18849 Difficulty: Medium Contributors: Admin...

tf.nn.conv2d_transpose 实例 及 解析

这个程序完全按照 https://github.com/tensorflow/tensorflow/blob/r0.11/tensorflow/core/kernels/conv_grad_ops.c...

Transpose File

194.Transpose File 题目: Given a text file file.txt, transpose its content.

leetcode-194 Transpose File

感觉很扯淡的一题,自己写的总是memory limit exceed或者time limit exceed memory limit exceed #!/bin/bash cols=$(head -n...

利用tf.nn.conv2d_transpose实现图片分辨率按指定倍数扩展

我有输入尺寸为 64x64x3 的图片,即高64, 宽64, 通道数3 我想通过transposed convolution 来使图片的高宽放大到2倍,即 128x128import tensorf...

Uva 10895 - Matrix Transpose

这个就是说有这么一个矩阵,好多0,让你求转制 sort预处理 之后想辙输出就好 第一遍遍历的点,发现各种特殊状况不好弄 AC的是枚举第几行 #include #include #includ...

Inverse transpose matrix

对normal进行变换不能直接乘以变换矩阵,必须乘以inverse transpose matrix 给定一个顶点的Normal n,以及过该点的切平面上的一个向量u,且有变换矩阵T。u,n是行向量...
  • seizeF
  • seizeF
  • 2011-08-21 18:16
  • 2465
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)