[spire] basic question: using Trig[Rational] and NRoot[Rational]

39 views
Skip to first unread message

Hugo Ferreira

unread,
Aug 7, 2015, 4:35:33 AM8/7/15
to Typelevel Users & Development List
Hi,

First and foremost my apologies if this is too basic a question. 
Any pointers to existing material is appreciated. 

So I am implementing some basic CAS functionality and am at the point were I need 
to deal with a numerical value expressed as a rationals. So looking at the documentation 
I figure I should use:

 class R extends Trig[Rational] with NRoot[Rational]
 
Of course this does not work as per the documentation. But whilst looking around for a 
solution I found references to a CReal for an older version of Spire. So it seems that it
is possible to approximate irrationals via the rationals and get at least Trig[_] working.

So my question is, how should I go about this? Can anyone point out any examples were 
I can implement the NRoot and Trig members using Rational?  

On another note. The documentation states that: 

  "Spire supports square roots and fractional powers via NRoot[A]"

and 

  "Approximate types like Double and BigDecimal have a built-in precision to which Spire can find roots."

So I would assume that NRoot[Double] or NRoot[BigDecimal] would work. However this is not so (see code below). 
Why? Am I missing some implicit import? Some thing for the case of Trig[Double]. Must be doing something wrong.

TIA,
HF

------------------------------------------
using: "org.spire-math" %% "spire" % "0.10.1"

import spire.algebra._   // provides algebraic type classes
import spire.math._      // provides functions, types, and type classes
import spire.implicits._ // provides infix operators, instances and conversions

class R2 extends NRoot[Double]

..................................................

[error] it has 2 unimplemented members.
[error] /** As seen from class R2, the missing signatures are as follows.
[error]  *  For convenience, these are usable as stub implementations.
[error]  */
[error]   def fpow(a: Double,b: Double): Double = ???
[error]   def nroot(a: Double,n: Int): Double = ???
[error] class R2 extends NRoot[Double]
[error]       ^


-----------------------------------------


Erik Osheim

unread,
Aug 7, 2015, 9:41:14 AM8/7/15
to Hugo Ferreira, Typelevel Users & Development List
Hi Hugo,

If you 'import spire.implicits._' that will provide a bunch of new
methods on Double/BigDecimal (using the relevant NRoot[_] instance)
which will allow you to take roots and such.

import spire.algebra.Trig
import spire.implicits._

(3.0).nroot(3) // cube-root of 3
// 1.4422495703074083

BigDecimal(3).nroot(3)
// 1.442249570307408382321638310780110

Trig[Double].sin(33.333)
// 0.9406427616458263

Trig[BigDecimal].sin(BigDecimal("33.333"))
// 0.9406427616458257562817995457628267102

It sounds like you didn't have that import set up.

You can use spire.math.Real to wrap Rational and support roots,
trigonometry, etc. That class is the successor to the CReal type you
mentioned. Here's an example:

import spire.math._
val rat: Rational = Rational(1, 3) // one third
val x: Real = Real(rat).sqrt

x.toRational
// 100588603464850855725768518207408495724057/174224571863520493293247799005065324265472

x.toRational(80)
// 174493411846198778282783/302231454903657293676544

x.toRational(40)
// 634803334273/1099511627776

You will have to choose how much precision you want the resulting
rational to be. The values 40, and 80, refer to the precision of the
denominator, i.e. 2^40 or 2^80.

In a follow-up email I can try to explain how to build a custom type
which supports NRoot[_] and Trig[_] but first I wanted to make sure I
answered your basic questions about using Rational. Let me know if
this makes sense and works for you.

Thanks,

-- Erik
> --
> You received this message because you are subscribed to the Google Groups "Typelevel Users & Development List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to typelevel+...@googlegroups.com.
> To post to this group, send email to type...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/typelevel/e7637136-e7cd-48b9-8e13-c8490ef0c318%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Hugo Ferreira

unread,
Aug 7, 2015, 10:57:19 AM8/7/15
to Typelevel Users & Development List, hugo6f...@gmail.com, er...@plastic-idolatry.com
Hi Erik,

Appreciate the help. 


On Friday, 7 August 2015 14:41:14 UTC+1, Erik Osheim wrote:
Hi Hugo,

If you 'import spire.implicits._' that will provide a bunch of new
methods on Double/BigDecimal (using the relevant NRoot[_] instance)
which will allow you to take roots and such.

  import spire.algebra.Trig
  import spire.implicits._

  (3.0).nroot(3) // cube-root of 3
  // 1.4422495703074083

  BigDecimal(3).nroot(3)
  // 1.442249570307408382321638310780110

  Trig[Double].sin(33.333)
  // 0.9406427616458263

  Trig[BigDecimal].sin(BigDecimal("33.333"))
  // 0.9406427616458257562817995457628267102

It sounds like you didn't have that import set up.


It working. So Spire and its imports are working fine. 
 
You can use spire.math.Real to wrap Rational and support roots,
trigonometry, etc. That class is the successor to the CReal type you
mentioned. Here's an example:

  import spire.math._
  val rat: Rational = Rational(1, 3) // one third
  val x: Real = Real(rat).sqrt

  x.toRational
  // 100588603464850855725768518207408495724057/174224571863520493293247799005065324265472

  x.toRational(80)
  // 174493411846198778282783/302231454903657293676544

  x.toRational(40)
  // 634803334273/1099511627776


This worked too. 
 
You will have to choose how much precision you want the resulting
rational to be. The values 40, and 80, refer to the precision of the
denominator, i.e. 2^40 or 2^80.


Pk. This is not an issue right now. Later I will have to learn how I can compare values in order decide this. 
 
In a follow-up email I can try to explain how to build a custom type
which supports NRoot[_] and Trig[_] but first I wanted to make sure I
answered your basic questions about using Rational. Let me know if
this makes sense and works for you.


Still haven't quite made sense of the Spire types but I am working on it. 
If it makes it any easier point me to an example or Spire source.

Thank you.  
Hugo
Reply all
Reply to author
Forward
0 new messages