<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Progressively increasing error in calculation in Intel® oneAPI Math Kernel Library</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Progressively-increasing-error-in-calculation/m-p/1093722#M23397</link>
    <description>&lt;P&gt;This function calculates determinant of a matrix:&lt;/P&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;real*8 function mklDet(A)
    real*8, intent(in), dimension(:,:) :: A         !&amp;lt; input matrix A
    real*8, dimension(size(A,1)) :: ipiv            !! pivot indices
    integer N, info, i                              !&amp;gt; dimension, status
    
    N = size(A, 1)
 
    ! DGETRF computes an LU factorization of a general M-by-N matrix A
    ! using partial pivoting with row interchanges.
    call DGETRF(n, n, A, n, ipiv, info)
    
    !&amp;lt; This part of code by adopted from intel software forum topic 309460
    mklDet = 0.d0
    if (info &amp;gt; 0) then
        return
    else if (info &amp;lt; 0) then
        print *, 'Fatal error calculating determinant!'
    end if
    mklDet = 1.d0
    do i = 1, n
        if (ipiv(i).ne.i) then
            mklDet = -mklDet * a(i,i)
        else
            mklDet = mklDet * a(i,i)
        endif
    end do
    !&amp;gt;
end function mklDet   &lt;/PRE&gt;

&lt;P&gt;Now if we try to calculate determinant of a matrix with which should be zero:&lt;/P&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;!! [[6.d10,  3.d10,  5.d10];
!!  [4.d10,  4.d10,  7.d10];
!!  [12.d10, 6.d10, 10.d10]];&lt;/PRE&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;print *, 'zero determinant' , mklDet(reshape((/ 6.d10, 3.d10, 5.d10, 4.d10, 4.d10, 7.d10, 12.d10, 6.d10, 10.d10 /),(/3, 3/)))&lt;/PRE&gt;

&lt;P&gt;as you can see the outcome is a very large number and not zero:&lt;/P&gt;

&lt;PRE class="brush:plain;"&gt; zero determinant -5.329070518200751E+015&lt;/PRE&gt;

&lt;P&gt;but if we use smaller numbers in the matrix&lt;/P&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;!! [[6.,  3.,  5.];
!!  [4.,  4.,  7.];
!!  [12., 6., 10.]];&lt;/PRE&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;print *, 'zero determinant' , mklDet(reshape((/ 6.d0, 3.d0, 5.d0, 4.d0, 4.d0, 7.d0, 12.d0, 6.d0, 10.d0 /),(/3, 3/)))&lt;/PRE&gt;

&lt;P&gt;The outcome is much closer to zero:&lt;/P&gt;

&lt;PRE class="brush:plain;"&gt; zero determinant  1.065814103640150E-014&lt;/PRE&gt;

&lt;P&gt;How do I improve the accuracy of my calculations?&lt;BR /&gt;
	&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Apr 2017 15:23:46 GMT</pubDate>
    <dc:creator>S__MPay</dc:creator>
    <dc:date>2017-04-27T15:23:46Z</dc:date>
    <item>
      <title>Progressively increasing error in calculation</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Progressively-increasing-error-in-calculation/m-p/1093722#M23397</link>
      <description>&lt;P&gt;This function calculates determinant of a matrix:&lt;/P&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;real*8 function mklDet(A)
    real*8, intent(in), dimension(:,:) :: A         !&amp;lt; input matrix A
    real*8, dimension(size(A,1)) :: ipiv            !! pivot indices
    integer N, info, i                              !&amp;gt; dimension, status
    
    N = size(A, 1)
 
    ! DGETRF computes an LU factorization of a general M-by-N matrix A
    ! using partial pivoting with row interchanges.
    call DGETRF(n, n, A, n, ipiv, info)
    
    !&amp;lt; This part of code by adopted from intel software forum topic 309460
    mklDet = 0.d0
    if (info &amp;gt; 0) then
        return
    else if (info &amp;lt; 0) then
        print *, 'Fatal error calculating determinant!'
    end if
    mklDet = 1.d0
    do i = 1, n
        if (ipiv(i).ne.i) then
            mklDet = -mklDet * a(i,i)
        else
            mklDet = mklDet * a(i,i)
        endif
    end do
    !&amp;gt;
end function mklDet   &lt;/PRE&gt;

&lt;P&gt;Now if we try to calculate determinant of a matrix with which should be zero:&lt;/P&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;!! [[6.d10,  3.d10,  5.d10];
!!  [4.d10,  4.d10,  7.d10];
!!  [12.d10, 6.d10, 10.d10]];&lt;/PRE&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;print *, 'zero determinant' , mklDet(reshape((/ 6.d10, 3.d10, 5.d10, 4.d10, 4.d10, 7.d10, 12.d10, 6.d10, 10.d10 /),(/3, 3/)))&lt;/PRE&gt;

&lt;P&gt;as you can see the outcome is a very large number and not zero:&lt;/P&gt;

&lt;PRE class="brush:plain;"&gt; zero determinant -5.329070518200751E+015&lt;/PRE&gt;

&lt;P&gt;but if we use smaller numbers in the matrix&lt;/P&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;!! [[6.,  3.,  5.];
!!  [4.,  4.,  7.];
!!  [12., 6., 10.]];&lt;/PRE&gt;

&lt;PRE class="brush:fortran;" style="font-family:Consolas;font-size:13;color:gainsboro;background:#1e1e1e;"&gt;print *, 'zero determinant' , mklDet(reshape((/ 6.d0, 3.d0, 5.d0, 4.d0, 4.d0, 7.d0, 12.d0, 6.d0, 10.d0 /),(/3, 3/)))&lt;/PRE&gt;

&lt;P&gt;The outcome is much closer to zero:&lt;/P&gt;

&lt;PRE class="brush:plain;"&gt; zero determinant  1.065814103640150E-014&lt;/PRE&gt;

&lt;P&gt;How do I improve the accuracy of my calculations?&lt;BR /&gt;
	&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2017 15:23:46 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Progressively-increasing-error-in-calculation/m-p/1093722#M23397</guid>
      <dc:creator>S__MPay</dc:creator>
      <dc:date>2017-04-27T15:23:46Z</dc:date>
    </item>
    <item>
      <title>Update: When I try same</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Progressively-increasing-error-in-calculation/m-p/1093723#M23398</link>
      <description>&lt;P&gt;Update: When I try same function with quad precision the error is exactly zero:&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;    print *, 'zero determinant double precision' , dblMklDet(reshape((/ 6.d0, 3.d0, 5.d0, 4.d0, 4.d0, 7.d0, 12.d0, 6.d0, 10.d0 /),(/3, 3/)))
    print *, 'zero determinant  quad  precision' , qprMklDet(reshape((/ 6.q0, 3.q0, 5.q0, 4.q0, 4.q0, 7.q0, 12.q0, 6.q0, 10.q0 /),(/3, 3/)))
&lt;/PRE&gt;

&lt;P&gt;The output is:&lt;/P&gt;

&lt;PRE class="brush:plain;"&gt; zero determinant double precision  1.065814103640150E-014
 zero determinant  quad  precision  0.000000000000000000000000000000000E+0000&lt;/PRE&gt;

&lt;P&gt;I don't understand what has caused the problem in double precision.&lt;/P&gt;

&lt;P&gt;Here is the source for quad precision version of my MklDet function:&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;      real(qp) function qprMklDet(A)
          real(qp), intent(in), dimension(:,:) :: A         !&amp;lt; input matrix A
          real(qp), dimension(size(A,1)) :: ipiv            !! pivot indices
          integer N, info, i                                !&amp;gt; dimension, status
          
          N = size(A, 1)
      
          ! DGETRF computes an LU factorization of a general M-by-N matrix A
          ! using partial pivoting with row interchanges.
          call DGETRF(n, n, A, n, ipiv, info)
          
          !&amp;lt; This part of code by adopted from intel software forum topic 309460
          qprMklDet = 0.d0
          if (info &amp;gt; 0) then
              return
          else if (info &amp;lt; 0) then
              print *, 'Fatal error calculating determinant!'
          end if
          qprMklDet = 1.d0
          do i = 1, n
              if (ipiv(i).ne.i) then
                  qprMklDet = -qprMklDet * a(i,i)
              else
                  qprMklDet = qprMklDet * a(i,i)
              endif
          end do
          !&amp;gt;
      end function qprMklDet     
&lt;/PRE&gt;

&lt;P&gt;And qp is&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;      use, intrinsic :: iso_fortran_env, only: REAL64, REAL128
      implicit none
      !==========================================
      ! C  o  n  s  t  a  n  t  s
      !==========================================
      !integer, parameter :: sp = REAL32
      integer, parameter :: dp = REAL64
      integer, parameter :: qp = REAL128
      !------------------------------------------
&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2017 16:33:56 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Progressively-increasing-error-in-calculation/m-p/1093723#M23398</guid>
      <dc:creator>S__MPay</dc:creator>
      <dc:date>2017-05-01T16:33:56Z</dc:date>
    </item>
    <item>
      <title>Hi,</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Progressively-increasing-error-in-calculation/m-p/1093724#M23399</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;Det calculation using LU is not stable numerically. E.g., it's clearly described at matlab page of Det function here:&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-size: 1em;"&gt;&lt;A href="https://www.mathworks.com/help/matlab/ref/det.html" target="_blank"&gt;https://www.mathworks.com/help/matlab/ref/det.html&lt;/A&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 1em;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;So, please just avoid calling LU with singular matrices. You can first use other functions like ?gecon in order to estimate condition number of the matrix.&lt;/P&gt;

&lt;P&gt;Regards,&lt;/P&gt;

&lt;P&gt;Konstantin&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2017 16:03:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Progressively-increasing-error-in-calculation/m-p/1093724#M23399</guid>
      <dc:creator>Konstantin_A_Intel</dc:creator>
      <dc:date>2017-05-08T16:03:00Z</dc:date>
    </item>
  </channel>
</rss>

