[jira] [Commented] (MATH-1617) FieldLUDecomposition with BigReal throw divide by zero error

2021-07-19 Thread Gilles Sadowski (Jira)


[ 
https://issues.apache.org/jira/browse/MATH-1617?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17383425#comment-17383425
 ] 

Gilles Sadowski commented on MATH-1617:
---

PR merged in commit 30a2593c2c9808d7ad118d8e120e8ce807df746a ("master" branch).

> FieldLUDecomposition with BigReal throw divide by zero error
> 
>
> Key: MATH-1617
> URL: https://issues.apache.org/jira/browse/MATH-1617
> Project: Commons Math
>  Issue Type: Bug
>Affects Versions: 3.6.1
>Reporter: Ng Tsz Sum
>Priority: Major
> Fix For: 4.0
>
> Attachments: MATH-1617.patch
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
> From 
> [https://stackoverflow.com/questions/68427448/how-to-find-the-inverse-of-a-matrix-using-apache-commons-math-library-in-java/68427869?noredirect=1#comment120932354_68427869]
> h3. Reproducible example
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class REPREX {
> public static void main(String[] args) {
> BigReal[][] leftMatrixData = new BigReal[][]{
> {new BigReal(1), new BigReal(0), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(0), new BigReal(1), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(1), new 
> BigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> //  Exception in thread "main" 
> org.apache.commons.math3.exception.MathArithmeticException: zero not allowed 
> here
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:255)
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:39)
> //  at 
> org.apache.commons.math3.linear.FieldLUDecomposition.(FieldLUDecomposition.java:160)
> //  at stackoverflow.math.matrix.REPREX.main(REPREX.java:18)
> }
> }
> {code}
> h3. Possible reason:
> In {{FieldLUDecomposition}} line 130-133
> {code:java}
> if (lu[nonZero][col].equals(field.getZero())) {
> // try to select a better permutation choice
> ++nonZero;
> }
> {code}
> Which produce incorrect result when {{lu[nonZeror][col]}} the BigDecimal val 
> has different scale 
>  to {{field.getZero()}} scale. as the {{BigReal#equals}} is comparing using 
> {{BigDecimal#equals}}
> h3. Workaround
> I tried to copy class {{BigReal}} and {{BigRealField}} to {{FixBigReal}} and 
> {{FixBigRealField}} and replace all {{BigReal}} to {{FixBigReal}} inside. 
> Then override {{FixBIgReal#equals}} and {{FixBIgReal#hashcode }}as
> {code:java}
> @Override
> public boolean equals(Object other) {
> if (this == other) {
> return true;
> }
> if (other instanceof FixBigReal) {
> return d.compareTo(((FixBigReal) other).d) == 0;
> }
> return false;
> }
> ...
> @Override
> public int hashCode() {
> return Double.hashCode(d.doubleValue());
> }
> {code}
> Then the below program will not throw error
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class MVE {
> public static void main(String[] args) {
> FixBigReal[][] leftMatrixData = new FixBigReal[][]{
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(1), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(1), new 
> FixBigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (MATH-1617) FieldLUDecomposition with BigReal throw divide by zero error

2021-07-19 Thread Ng Tsz Sum (Jira)


[ 
https://issues.apache.org/jira/browse/MATH-1617?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17383370#comment-17383370
 ] 

Ng Tsz Sum commented on MATH-1617:
--

Just created PR

https://github.com/apache/commons-math/pull/192

> FieldLUDecomposition with BigReal throw divide by zero error
> 
>
> Key: MATH-1617
> URL: https://issues.apache.org/jira/browse/MATH-1617
> Project: Commons Math
>  Issue Type: Bug
>Affects Versions: 3.6.1
>Reporter: Ng Tsz Sum
>Priority: Major
> Fix For: 4.0
>
> Attachments: MATH-1617.patch
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
> From 
> [https://stackoverflow.com/questions/68427448/how-to-find-the-inverse-of-a-matrix-using-apache-commons-math-library-in-java/68427869?noredirect=1#comment120932354_68427869]
> h3. Reproducible example
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class REPREX {
> public static void main(String[] args) {
> BigReal[][] leftMatrixData = new BigReal[][]{
> {new BigReal(1), new BigReal(0), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(0), new BigReal(1), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(1), new 
> BigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> //  Exception in thread "main" 
> org.apache.commons.math3.exception.MathArithmeticException: zero not allowed 
> here
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:255)
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:39)
> //  at 
> org.apache.commons.math3.linear.FieldLUDecomposition.(FieldLUDecomposition.java:160)
> //  at stackoverflow.math.matrix.REPREX.main(REPREX.java:18)
> }
> }
> {code}
> h3. Possible reason:
> In {{FieldLUDecomposition}} line 130-133
> {code:java}
> if (lu[nonZero][col].equals(field.getZero())) {
> // try to select a better permutation choice
> ++nonZero;
> }
> {code}
> Which produce incorrect result when {{lu[nonZeror][col]}} the BigDecimal val 
> has different scale 
> to {{field.getZero()}} scale. as the {{BigReal#equals}} is comparing using 
> {{BigDecimal#equals}}
> h3. Workaround
> I tried to copy class {{BigReal}} and {{BigRealField}} to {{FixBigReal}} and 
> {{FixBigRealField}} and replace all {{BigReal}} to {{FixBigReal}} inside. 
> Then override {{FixBIgReal#equals}} as 
> {code:java}
> @Override
> public boolean equals(Object other) {
> if (this == other) {
> return true;
> }
> if (other instanceof FixBigReal) {
> return d.compareTo(((FixBigReal) other).d) == 0;
> }
> return false;
> }
> {code}
> Then the below program will not throw error
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class MVE {
> public static void main(String[] args) {
> FixBigReal[][] leftMatrixData = new FixBigReal[][]{
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(1), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(1), new 
> FixBigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (MATH-1617) FieldLUDecomposition with BigReal throw divide by zero error

2021-07-19 Thread Ng Tsz Sum (Jira)


[ 
https://issues.apache.org/jira/browse/MATH-1617?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17383349#comment-17383349
 ] 

Ng Tsz Sum commented on MATH-1617:
--

[~erans], just revisited my fix, it seems to have some problem as 
{{BigReal#equals}} method is not consistent with {{hashCode}}. I am thinking to 
change {{hashCode}} method of {{BigReal}} to make both consistent referring to 
https://github.com/google/auto/issues/411

> FieldLUDecomposition with BigReal throw divide by zero error
> 
>
> Key: MATH-1617
> URL: https://issues.apache.org/jira/browse/MATH-1617
> Project: Commons Math
>  Issue Type: Bug
>Affects Versions: 3.6.1
>Reporter: Ng Tsz Sum
>Priority: Major
> Fix For: 4.0
>
> Attachments: MATH-1617.patch
>
>
> From 
> [https://stackoverflow.com/questions/68427448/how-to-find-the-inverse-of-a-matrix-using-apache-commons-math-library-in-java/68427869?noredirect=1#comment120932354_68427869]
> h3. Reproducible example
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class REPREX {
> public static void main(String[] args) {
> BigReal[][] leftMatrixData = new BigReal[][]{
> {new BigReal(1), new BigReal(0), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(0), new BigReal(1), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(1), new 
> BigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> //  Exception in thread "main" 
> org.apache.commons.math3.exception.MathArithmeticException: zero not allowed 
> here
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:255)
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:39)
> //  at 
> org.apache.commons.math3.linear.FieldLUDecomposition.(FieldLUDecomposition.java:160)
> //  at stackoverflow.math.matrix.REPREX.main(REPREX.java:18)
> }
> }
> {code}
> h3. Possible reason:
> In {{FieldLUDecomposition}} line 130-133
> {code:java}
> if (lu[nonZero][col].equals(field.getZero())) {
> // try to select a better permutation choice
> ++nonZero;
> }
> {code}
> Which produce incorrect result when {{lu[nonZeror][col]}} the BigDecimal val 
> has different scale 
> to {{field.getZero()}} scale. as the {{BigReal#equals}} is comparing using 
> {{BigDecimal#equals}}
> h3. Workaround
> I tried to copy class {{BigReal}} and {{BigRealField}} to {{FixBigReal}} and 
> {{FixBigRealField}} and replace all {{BigReal}} to {{FixBigReal}} inside. 
> Then override {{FixBIgReal#equals}} as 
> {code:java}
> @Override
> public boolean equals(Object other) {
> if (this == other) {
> return true;
> }
> if (other instanceof FixBigReal) {
> return d.compareTo(((FixBigReal) other).d) == 0;
> }
> return false;
> }
> {code}
> Then the below program will not throw error
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class MVE {
> public static void main(String[] args) {
> FixBigReal[][] leftMatrixData = new FixBigReal[][]{
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(1), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(1), new 
> FixBigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (MATH-1617) FieldLUDecomposition with BigReal throw divide by zero error

2021-07-18 Thread Gilles Sadowski (Jira)


[ 
https://issues.apache.org/jira/browse/MATH-1617?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17382856#comment-17382856
 ] 

Gilles Sadowski commented on MATH-1617:
---

bq. I have no access

IIUC, you have to create your own fork on GH.
For sure the [repository|https://github.com/apache/commons-math/] exists. ;-)

Patch merged in commit 0b56f2cf54c577ee92d1411b2e5ba42a02bf8a02 ("master" 
branch).  Thanks!

> FieldLUDecomposition with BigReal throw divide by zero error
> 
>
> Key: MATH-1617
> URL: https://issues.apache.org/jira/browse/MATH-1617
> Project: Commons Math
>  Issue Type: Bug
>Affects Versions: 3.6.1
>Reporter: Ng Tsz Sum
>Priority: Major
> Attachments: MATH-1617.patch
>
>
> From 
> [https://stackoverflow.com/questions/68427448/how-to-find-the-inverse-of-a-matrix-using-apache-commons-math-library-in-java/68427869?noredirect=1#comment120932354_68427869]
> h3. Reproducible example
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class REPREX {
> public static void main(String[] args) {
> BigReal[][] leftMatrixData = new BigReal[][]{
> {new BigReal(1), new BigReal(0), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(0), new BigReal(1), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(1), new 
> BigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> //  Exception in thread "main" 
> org.apache.commons.math3.exception.MathArithmeticException: zero not allowed 
> here
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:255)
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:39)
> //  at 
> org.apache.commons.math3.linear.FieldLUDecomposition.(FieldLUDecomposition.java:160)
> //  at stackoverflow.math.matrix.REPREX.main(REPREX.java:18)
> }
> }
> {code}
> h3. Possible reason:
> In {{FieldLUDecomposition}} line 130-133
> {code:java}
> if (lu[nonZero][col].equals(field.getZero())) {
> // try to select a better permutation choice
> ++nonZero;
> }
> {code}
> Which produce incorrect result when {{lu[nonZeror][col]}} the BigDecimal val 
> has different scale 
> to {{field.getZero()}} scale. as the {{BigReal#equals}} is comparing using 
> {{BigDecimal#equals}}
> h3. Workaround
> I tried to copy class {{BigReal}} and {{BigRealField}} to {{FixBigReal}} and 
> {{FixBigRealField}} and replace all {{BigReal}} to {{FixBigReal}} inside. 
> Then override {{FixBIgReal#equals}} as 
> {code:java}
> @Override
> public boolean equals(Object other) {
> if (this == other) {
> return true;
> }
> if (other instanceof FixBigReal) {
> return d.compareTo(((FixBigReal) other).d) == 0;
> }
> return false;
> }
> {code}
> Then the below program will not throw error
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class MVE {
> public static void main(String[] args) {
> FixBigReal[][] leftMatrixData = new FixBigReal[][]{
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(1), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(1), new 
> FixBigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (MATH-1617) FieldLUDecomposition with BigReal throw divide by zero error

2021-07-18 Thread Ng Tsz Sum (Jira)


[ 
https://issues.apache.org/jira/browse/MATH-1617?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17382849#comment-17382849
 ] 

Ng Tsz Sum commented on MATH-1617:
--

[~erans] , I tried to push a new branch for PR but found I have no access:
{quote}ERROR: Permission to apache/commons-math.git denied to samabcde.
 fatal: Could not read from remote repository.

Please make sure you have the correct access rights
 and the repository exists.
{quote}
So I attached the patch instead.

> FieldLUDecomposition with BigReal throw divide by zero error
> 
>
> Key: MATH-1617
> URL: https://issues.apache.org/jira/browse/MATH-1617
> Project: Commons Math
>  Issue Type: Bug
>Affects Versions: 3.6.1
>Reporter: Ng Tsz Sum
>Priority: Major
> Attachments: MATH-1617.patch
>
>
> From 
> [https://stackoverflow.com/questions/68427448/how-to-find-the-inverse-of-a-matrix-using-apache-commons-math-library-in-java/68427869?noredirect=1#comment120932354_68427869]
> h3. Reproducible example
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class REPREX {
> public static void main(String[] args) {
> BigReal[][] leftMatrixData = new BigReal[][]{
> {new BigReal(1), new BigReal(0), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(0), new BigReal(1), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(1), new 
> BigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> //  Exception in thread "main" 
> org.apache.commons.math3.exception.MathArithmeticException: zero not allowed 
> here
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:255)
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:39)
> //  at 
> org.apache.commons.math3.linear.FieldLUDecomposition.(FieldLUDecomposition.java:160)
> //  at stackoverflow.math.matrix.REPREX.main(REPREX.java:18)
> }
> }
> {code}
> h3. Possible reason:
> In {{FieldLUDecomposition}} line 130-133
> {code:java}
> if (lu[nonZero][col].equals(field.getZero())) {
> // try to select a better permutation choice
> ++nonZero;
> }
> {code}
> Which produce incorrect result when {{lu[nonZeror][col]}} the BigDecimal val 
> has different scale 
> to {{field.getZero()}} scale. as the {{BigReal#equals}} is comparing using 
> {{BigDecimal#equals}}
> h3. Workaround
> I tried to copy class {{BigReal}} and {{BigRealField}} to {{FixBigReal}} and 
> {{FixBigRealField}} and replace all {{BigReal}} to {{FixBigReal}} inside. 
> Then override {{FixBIgReal#equals}} as 
> {code:java}
> @Override
> public boolean equals(Object other) {
> if (this == other) {
> return true;
> }
> if (other instanceof FixBigReal) {
> return d.compareTo(((FixBigReal) other).d) == 0;
> }
> return false;
> }
> {code}
> Then the below program will not throw error
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class MVE {
> public static void main(String[] args) {
> FixBigReal[][] leftMatrixData = new FixBigReal[][]{
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(1), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(1), new 
> FixBigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (MATH-1617) FieldLUDecomposition with BigReal throw divide by zero error

2021-07-18 Thread Gilles Sadowski (Jira)


[ 
https://issues.apache.org/jira/browse/MATH-1617?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17382817#comment-17382817
 ] 

Gilles Sadowski commented on MATH-1617:
---

Thanks for the report.
Could you please provide the fix and test case (as a JUnit test) through a 
patch ([PR|https://github.com/apache/commons-math/], or diff file) against the 
["master" branch|https://gitbox.apache.org/repos/asf?p=commons-math.git]?

> FieldLUDecomposition with BigReal throw divide by zero error
> 
>
> Key: MATH-1617
> URL: https://issues.apache.org/jira/browse/MATH-1617
> Project: Commons Math
>  Issue Type: Bug
>Affects Versions: 3.6.1
>Reporter: Ng Tsz Sum
>Priority: Major
>
> From 
> [https://stackoverflow.com/questions/68427448/how-to-find-the-inverse-of-a-matrix-using-apache-commons-math-library-in-java/68427869?noredirect=1#comment120932354_68427869]
> h3. Reproducible example
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class REPREX {
> public static void main(String[] args) {
> BigReal[][] leftMatrixData = new BigReal[][]{
> {new BigReal(1), new BigReal(0), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(0), new BigReal(1), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(0), new 
> BigReal(0)},
> {new BigReal(1), new BigReal(1), new BigReal(1), new 
> BigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> //  Exception in thread "main" 
> org.apache.commons.math3.exception.MathArithmeticException: zero not allowed 
> here
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:255)
> //  at org.apache.commons.math3.util.BigReal.divide(BigReal.java:39)
> //  at 
> org.apache.commons.math3.linear.FieldLUDecomposition.(FieldLUDecomposition.java:160)
> //  at stackoverflow.math.matrix.REPREX.main(REPREX.java:18)
> }
> }
> {code}
> h3. Possible reason:
> In {{FieldLUDecomposition}} line 130-133
> {code:java}
> if (lu[nonZero][col].equals(field.getZero())) {
> // try to select a better permutation choice
> ++nonZero;
> }
> {code}
> Which produce incorrect result when {{lu[nonZeror][col]}} the BigDecimal val 
> has different scale 
> to {{field.getZero()}} scale. as the {{BigReal#equals}} is comparing using 
> {{BigDecimal#equals}}
> h3. Workaround
> I tried to copy class {{BigReal}} and {{BigRealField}} to {{FixBigReal}} and 
> {{FixBigRealField}} and replace all {{BigReal}} to {{FixBigReal}} inside. 
> Then override {{FixBIgReal#equals}} as 
> {code:java}
> @Override
> public boolean equals(Object other) {
> if (this == other) {
> return true;
> }
> if (other instanceof FixBigReal) {
> return d.compareTo(((FixBigReal) other).d) == 0;
> }
> return false;
> }
> {code}
> Then the below program will not throw error
> {code:java}
> import org.apache.commons.math3.linear.FieldLUDecomposition;
> import org.apache.commons.math3.linear.FieldMatrix;
> import org.apache.commons.math3.linear.MatrixUtils;
> import org.apache.commons.math3.util.BigReal;
> public class MVE {
> public static void main(String[] args) {
> FixBigReal[][] leftMatrixData = new FixBigReal[][]{
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(0), new FixBigReal(1), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(0), new 
> FixBigReal(0)},
> {new FixBigReal(1), new FixBigReal(1), new FixBigReal(1), new 
> FixBigReal(1)},
> };
> FieldMatrix leftMatrix = 
> MatrixUtils.createFieldMatrix(leftMatrixData);
> FieldMatrix leftMatrixInverse = new 
> FieldLUDecomposition<>(leftMatrix)
> .getSolver()
> .getInverse();
> }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)