Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

66%
+2 −0
Challenges Compute the determinant

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...

posted 3y ago by user‭  ·  edited 3y ago by user‭

Answer
#4: Post edited by user avatar user‭ · 2021-08-29T14:03:59Z (over 3 years ago)
  • # 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 by user avatar user‭ · 2021-08-27T22:25:22Z (over 3 years ago)
  • # 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 by user avatar user‭ · 2021-08-27T22:19:43Z (over 3 years ago)
  • # Scala, 137 bytes
  • ```scala
  • def f(m:Seq[Seq[Double]]):Double=if(m.size<2)m(0)(0)else
  • m.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.
#1: Initial revision by user avatar user‭ · 2021-08-27T22:10:20Z (over 3 years ago)
# Scala, 137 bytes

```scala
def f(m:Seq[Seq[Double]]):Double=if(m.size<2)m(0)(0)else
m.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)