Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4626678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:27:21+00:00 2026-05-22T03:27:21+00:00

I’m looking for a simple CAS system for scala. It should have the following

  • 0

I’m looking for a simple CAS system for scala.

It should have the following features:

  • give access to the abstract syntax tree (preferably via case classes for easy matching)
  • parse String to AST
  • simplify expressions

If none exists and I have to write something basic myself, what’s the best representation?

I’m thinking something like this:

abstract trait Term
{
  def simplify:Term
  def evaluate(assignment:Var => Double):Double
  def derivative:Term
}

case class Const(c:Int) extends Term
case class Var(x:String) extends Term

case class Negate(x:Term) extends Term
case class Subtract(x:Term, y:Term) extends Term
case class Divide(x:Term, y:Term) extends Term


object Add { def apply(x:Term*):Add = Add(x.toList) }
case class Add(xs : List[Term]) extends Term

object Multiply { def apply(x:Term*):Multiply = Multiply(x.toList) }
case class Multiply(xs:List[Term]) extends Term

case class Power(x:Term, y:Term) extends Term
case class Exp(x:Term) extends Term

I’d implement the simplification algorithm described here, which seems tedious. (But maybe tedium is unavoidable when it comes to simplifying algebraic expressions?)

Some critiques of this specific implementation are:

  • I’ll be recursively calling simplify all over the place on the arguments to the case classes (seems like it could be centralized somehow)
  • Dealing with the varargs / List arguments to Add and Mutliply seems like it could get messy
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:27:22+00:00Added an answer on May 22, 2026 at 3:27 am

    I don’t know of an existing CAS for Scala.

    When doing language processing I normally find it much more pleasant to use pattern matching over a sealed hierarchy rather than OO style polymorphism. Since adding new term types is rare (that means a change of language) and adding new operations common this side of the expression problem seems to fit better.

    sealed trait Term
    case class Const(c : Double) extends Term
    case class Var(x : String) extends Term
    case class Negate(x : Term) extends Term
    case class Multiply(xs : List[Term]) extends Term
    // etc
    
    object CAS {
    
      // I assume that the assignment map may be incomplete, thus
      // evaluation is really a partial substitution and then simplification
      def evaluate(t : Term, assignment : Var => Option[Double]) : Term = t match {
        case _ : Const => t
        case v : Var => assignment(v) map Const getOrElse v
        case Negate(x) => evaluate(Multiply(Const(-1) :: evaluate(x, assignment) :: Nil), assignment)
        case Multiply(ts) => {
          val evalTs = ts map { t => evaluate(t, assignment) }
          val flattened = evalTs flatMap {
             case Multiply(subs) => subs
             case t => List(t)
          }
          val constTotal = Const((flattened collect { case Const(c) => c }).product)
          val otherTerms = flattened filter { case t : Const => false; case _ => true }
          (constTotal, otherTerms) match {
             case (Const(0), _) => Const(0)
             case (Const(1), Nil) => Const(1)
             case (Const(1), _) => Multiply(otherTerms)
             case _ => Multiply(constTotal +: otherTerms)
          }
        }
        // etc
    
      }
    
      private val emptyAssignment : (Var => Option[Double]) = { x : Var => None }
    
      // simplfication is just evaluation with an empty assignment
      def simplify(t : Term) : Term = evaluate(t, emptyAssignment)
    }
    

    One bit of technology I’ve been meaning to learn about but haven’t is attribute grammars. They’re supposed to take much of the tedium out of this kind of AST processing. See kiama http://code.google.com/p/kiama/ for a Scala implementation

    By the way, while I do use doubles here for your domain you might be better off using a “big rational” – a pair of BigIntegers. They’re slow but very precise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.