微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python:SciPy DCT 不适用于大型矩阵

如何解决Python:SciPy DCT 不适用于大型矩阵

我需要计算一个大矩阵的 DCT。我的代码似乎适用于较小的矩阵,但对于大小为 50000 x 50000 的矩阵会引发以下错误

import SwiftUI

struct ZoomableScrollView<Content: View>: UIViewRepresentable {
  private var content: Content

  init(@viewbuilder content: () -> Content) {
    self.content = content()
  }

  func makeUIView(context: Context) -> UIScrollView {
    // set up the UIScrollView
    let scrollView = UIScrollView()
    scrollView.delegate = context.coordinator  // for viewForZooming(in:)
    scrollView.maximumZoomScale = 20
    scrollView.minimumZoomScale = 1
    scrollView.bouncesZoom = true

    // create a UIHostingController to hold our SwiftUI content
    let hostedView = context.coordinator.hostingController.view!
    hostedView.translatesAutoresizingMaskIntoConstraints = true
    hostedView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
    hostedView.frame = scrollView.bounds
    hostedView.backgroundColor = .black
    scrollView.addSubview(hostedView)

    return scrollView
  }

  func makeCoordinator() -> Coordinator {
    return Coordinator(hostingController: UIHostingController(rootView: self.content))
  }

  func updateUIView(_ uiView: UIScrollView,context: Context) {
    // update the hosting controller's SwiftUI content
    context.coordinator.hostingController.rootView = self.content
    assert(context.coordinator.hostingController.view.superview == uiView)
  }

  // MARK: - Coordinator

  class Coordinator: NSObject,uiscrollviewdelegate {
    var hostingController: UIHostingController<Content>

    init(hostingController: UIHostingController<Content>) {
      self.hostingController = hostingController
    }

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
      return hostingController.view
    }
  }
}
error: (n>0&&n<=size(x)) Failed for the 1st keyword n: ddct2:n=50000

如何解决这个问题?非常感谢。

解决方法

使用 scipy.fft(不是 fftpack)似乎对我有用:

import numpy as np
import scipy.fft as fft

x = np.random.normal(size=(50000,50000))
y = fft.dct(x)

但是请注意,大小为 50000 的方形 ndarray 将需要超过 20 GB。您可能会遇到一次性处理这么多数据的问题。

版本说明:Python 3.9.2、NumPy 1.19.3、SciPy 1.6.1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。