Sha256: 77408af3fb8fc3ed19cab213f4ddf6fd61afa59e27326e0d76d6ac9d8836098e

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Numo::Linalg do
  describe 'lu_fact' do
    let(:m) { 5 }
    let(:n) { 3 }
    let(:mat_a) { rand_rect_real_mat(m, n) }
    let(:mat_b) { rand_rect_complex_mat(m, n) }

    def permutation_mat(ipiv)
      Numo::DFloat.eye(m).tap do |mat|
        ipiv.to_a.each_with_index { |a, b| mat[[a - 1, b], true] = mat[[b, a - 1], true].dup }
      end
    end

    it 'calculates the LU factorization of a rectangular real matrix' do
      lu, ipiv = described_class.lu_fact(mat_a)
      mat_l = lu.tril.tap { |mat| mat[mat.diag_indices(0)] = 1.0 }
      mat_u = lu.triu[0...n, 0...n]
      mat_p = permutation_mat(ipiv)
      expect((mat_p.dot(mat_a) - mat_l.dot(mat_u)).abs.max).to be < ERR_TOL
    end

    it 'calculates the LU factorization of a rectangular complex matrix' do
      lu, ipiv = described_class.lu_fact(mat_b)
      mat_l = lu.tril.tap { |mat| mat[mat.diag_indices(0)] = 1.0 }
      mat_u = lu.triu[0...n, 0...n]
      mat_p = permutation_mat(ipiv)
      expect((mat_p.dot(mat_b) - mat_l.dot(mat_u)).abs.max).to be < ERR_TOL
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
numo-linalg-0.1.7 spec/linalg/function/lu_fact_spec.rb
numo-linalg-0.1.6 spec/linalg/function/lu_fact_spec.rb
numo-linalg-0.1.5 spec/linalg/function/lu_fact_spec.rb
numo-linalg-0.1.4 spec/linalg/function/lu_fact_spec.rb
numo-linalg-0.1.3 spec/linalg/function/lu_fact_spec.rb