Post History
Scala, 130 125 bytes Saved 5 bytes by returning 1 like Hakerh400's great answer def f(m:Seq[Seq[Double]]):Double=if(m.size<1)1 else(m.indices:\.0){(i,a)=>m(0)(i)*f(m.tail.map(r=>r.take(i...
Answer
#4: Post edited
# Scala, 130 bytes- ```scala
def f(m:Seq[Seq[Double]]):Double=if(m.size<2)m(0)(0)else(m.indices:\.0){(i,a)=>m(0)(i)*f(m.tail.map(r=>r.take(i)++r.drop(i+1)))-a}- ```
[Try it in Scastie!](https://scastie.scala-lang.org/WYvEZcqFTDO5Vym6s7FIYA)- A recursive function that uses the first row for the Laplace expansion. Explanation coming soon.
- ```
- def f(m: Seq[Seq[Double]]): Double =
if (m.size < 2) //If it's a singleton matrix,m(0)(0) //Return the number inside- else
- //Otherwise, fold right over [0..m.size-1]
- //The initial accumulator is 0.0
- (m.indices :\ .0) {
- (i, a) =>
- //Multiply the entry by
- m(0)(i) *
- //The determinant of the smaller matrix
- f(
- //Drop the first row
- m.tail.map(
- //And for each row r, drop column i (0-indexed)
- r => r.take(i) ++ r.drop(i + 1)
- )
- )
- //Subtract the accumulator from that to invert
- //its sign each time
- - a
- }
- ```
- # Scala, <s>130</s> 125 bytes
- Saved 5 bytes by returning 1 like [Hakerh400's great answer](https://codegolf.codidact.com/posts/283789/283819#answer-283819)
- ```scala
- def f(m:Seq[Seq[Double]]):Double=if(m.size<1)1 else(m.indices:\.0){(i,a)=>m(0)(i)*f(m.tail.map(r=>r.take(i)++r.drop(i+1)))-a}
- ```
- [Try it in Scastie!](https://scastie.scala-lang.org/MzFMEJYhRdicn9z7ELWFaw)
- A recursive function that uses the first row for the Laplace expansion. Explanation coming soon.
- ```
- def f(m: Seq[Seq[Double]]): Double =
- if (m.size < 1) 1 //If it's empty, return 1
- else
- //Otherwise, fold right over [0..m.size-1]
- //The initial accumulator is 0.0
- (m.indices :\ .0) {
- (i, a) =>
- //Multiply the entry by
- m(0)(i) *
- //The determinant of the smaller matrix
- f(
- //Drop the first row
- m.tail.map(
- //And for each row r, drop column i (0-indexed)
- r => r.take(i) ++ r.drop(i + 1)
- )
- )
- //Subtract the accumulator from that to invert
- //its sign each time
- - a
- }
- ```
#3: Post edited
- # Scala, 130 bytes
- ```scala
- def f(m:Seq[Seq[Double]]):Double=if(m.size<2)m(0)(0)else(m.indices:\.0){(i,a)=>m(0)(i)*f(m.tail.map(r=>r.take(i)++r.drop(i+1)))-a}
- ```
- [Try it in Scastie!](https://scastie.scala-lang.org/WYvEZcqFTDO5Vym6s7FIYA)
A recursive function. Explanation coming soon.
- # Scala, 130 bytes
- ```scala
- def f(m:Seq[Seq[Double]]):Double=if(m.size<2)m(0)(0)else(m.indices:\.0){(i,a)=>m(0)(i)*f(m.tail.map(r=>r.take(i)++r.drop(i+1)))-a}
- ```
- [Try it in Scastie!](https://scastie.scala-lang.org/WYvEZcqFTDO5Vym6s7FIYA)
- A recursive function that uses the first row for the Laplace expansion. Explanation coming soon.
- ```
- def f(m: Seq[Seq[Double]]): Double =
- if (m.size < 2) //If it's a singleton matrix,
- m(0)(0) //Return the number inside
- else
- //Otherwise, fold right over [0..m.size-1]
- //The initial accumulator is 0.0
- (m.indices :\ .0) {
- (i, a) =>
- //Multiply the entry by
- m(0)(i) *
- //The determinant of the smaller matrix
- f(
- //Drop the first row
- m.tail.map(
- //And for each row r, drop column i (0-indexed)
- r => r.take(i) ++ r.drop(i + 1)
- )
- )
- //Subtract the accumulator from that to invert
- //its sign each time
- - a
- }
- ```
#2: Post edited
# Scala, 137 bytes- ```scala
def f(m:Seq[Seq[Double]]):Double=if(m.size<2)m(0)(0)elsem.indices.map{i=>(1-i%2*2)*f(m.tail.map(r=>r.take(i)++r.drop(i+1)))*m(0)(i)}.sum- ```
[Try it in Scastie!](https://scastie.scala-lang.org/NEBn7KRYQAqLAawHHWEdyQ)
- # Scala, 130 bytes
- ```scala
- def f(m:Seq[Seq[Double]]):Double=if(m.size<2)m(0)(0)else(m.indices:\.0){(i,a)=>m(0)(i)*f(m.tail.map(r=>r.take(i)++r.drop(i+1)))-a}
- ```
- [Try it in Scastie!](https://scastie.scala-lang.org/WYvEZcqFTDO5Vym6s7FIYA)
- A recursive function. Explanation coming soon.