-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5387204
commit a956e83
Showing
9 changed files
with
153 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/palaiologos/kamilalisp/runtime/flt64/Flt64Inv.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package palaiologos.kamilalisp.runtime.flt64; | ||
|
||
import palaiologos.kamilalisp.atom.Atom; | ||
import palaiologos.kamilalisp.atom.Environment; | ||
import palaiologos.kamilalisp.atom.Lambda; | ||
import palaiologos.kamilalisp.atom.PrimitiveFunction; | ||
import palaiologos.kamilalisp.runtime.array.Rank; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Flt64Inv extends PrimitiveFunction implements Lambda { | ||
@Override | ||
public Atom apply(Environment env, List<Atom> args) { | ||
Atom a1 = args.get(0); | ||
if (Rank.computeRank(a1) != 2) { | ||
throw new RuntimeException("Expected a matrix of rank 2."); | ||
} | ||
|
||
List<List<Atom>> l1 = a1.getList().stream().map(Atom::getList).toList(); | ||
if (l1.stream().anyMatch(x -> x.size() != l1.get(0).size())) { | ||
throw new RuntimeException("Expected a square matrix."); | ||
} | ||
|
||
double[][] A = l1.stream().map(x -> x.stream().mapToDouble(Flt64Base::toFlt64).toArray()).toArray(double[][]::new); | ||
|
||
double invdet = 1 / Flt64Det.det(A); | ||
for(int i = 0; i < A.length; i++) { | ||
for(int j = 0; j < A.length; j++) { | ||
A[i][j] *= invdet; | ||
} | ||
} | ||
|
||
return new Atom(Arrays.stream(A).map(x -> new Atom(Arrays.stream(x).mapToObj(Flt64Base::toAtom).toList())).toList()); | ||
} | ||
|
||
@Override | ||
protected String name() { | ||
return "flt64:invert"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/palaiologos/kamilalisp/runtime/math/num/I.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package palaiologos.kamilalisp.runtime.math.num; | ||
|
||
import palaiologos.kamilalisp.atom.Atom; | ||
import palaiologos.kamilalisp.atom.Environment; | ||
import palaiologos.kamilalisp.atom.Lambda; | ||
import palaiologos.kamilalisp.atom.PrimitiveFunction; | ||
|
||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class I extends PrimitiveFunction implements Lambda { | ||
@Override | ||
public Atom apply(Environment env, List<Atom> args) { | ||
assertArity(args, 1); | ||
int size = args.get(0).getInteger().intValueExact(); | ||
List<Atom> result = new ArrayList<>(); | ||
for(int i = 0; i < size; i++) { | ||
List<Atom> row = new ArrayList<>(); | ||
for(int j = 0; j < size; j++) | ||
row.add(new Atom(BigInteger.valueOf(i == j ? 1 : 0))); | ||
result.add(new Atom(row)); | ||
} | ||
return new Atom(result); | ||
} | ||
|
||
@Override | ||
protected String name() { | ||
return "num:I"; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/palaiologos/kamilalisp/runtime/math/num/Inv.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package palaiologos.kamilalisp.runtime.math.num; | ||
|
||
import palaiologos.kamilalisp.atom.*; | ||
import palaiologos.kamilalisp.runtime.array.Rank; | ||
import palaiologos.kamilalisp.runtime.math.Slash; | ||
import palaiologos.kamilalisp.runtime.math.Star; | ||
|
||
import java.util.List; | ||
|
||
public class Inv extends PrimitiveFunction implements Lambda { | ||
@Override | ||
public Atom apply(Environment env, List<Atom> args) { | ||
Atom a1 = args.get(0); | ||
if (Rank.computeRank(a1) != 2) { | ||
throw new RuntimeException("Expected a matrix of rank 2."); | ||
} | ||
|
||
List<List<Atom>> l1 = a1.getList().stream().map(Atom::getList).toList(); | ||
Atom result = Det.det(env.getMathContext(), l1); | ||
return Star.multiply2(Slash.quot1(env, result), a1); | ||
} | ||
|
||
@Override | ||
protected String name() { | ||
return "num:invert"; | ||
} | ||
} |