# Symbolic sqrt(2) and sin(pi)

**URL:** https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633
**Category:** Specific Domains
**Tags:** symbolic
**Created:** [April 4, 2021, 7:03pm UTC](https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633 "2021-04-04T19:03:25Z")
**Posts on this page:** 5
**Page:** 2

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 6, 2021, 12:47am UTC](https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633/21 "2021-04-06T00:47:46Z")

</div>

Sorry, I thought it was pretty clear but the threads are long now. We need to capture the literal `2` and make it `Literal(2)`. But then the issue is that we want `Literal{T} <: T` semantics. I.e. in `promote_type` it should use the promotions of `T` to build the value that is finally used in its rule applications or in its generated code. But not just `promote_type`, but every function. Without `Literal{T} <: T` semantics, that would require the symbolic library mimics `promote_type`, `sin`, and etc. dispatches of every number type, which then would mean we only would support `Literal{T}` for chosen types. So for now you can create the number with the type that you want, i.e. `Term(sqrt,[big(2)])` and when the whole `Sym{T} <: T` passes are working `Literal` will just be a new `istree` under the same semantics and that would do this automatically.

`Sym{T} <: T` of course has been talked about at length on these forums and elsewhere with the solution to be to build it in the tracing environment. We just need to make any type satisfying some trait be captured to automatically have these semantics.

---

<div class="post-metadata">

### Author: ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)
#### Post date: [August 23, 2021, 1:48pm UTC](https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633/22 "2021-08-23T13:48:56Z")

</div>

Just started learning Symbolics.jl a few weeks ago.

Why wouldn’t something like `@register sqrt(x::Real)` work?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [August 23, 2021, 3:37pm UTC](https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633/23 "2021-08-23T15:37:04Z")

</div>

> [@kapple](#):
>
> Why wouldn’t something like `@register sqrt(x::Real)` work?

It would be piracy, equivalent to:

```julia-auto
sqrt(x::Real) = Term(sqrt,[x])

```

and now something that wants to specialize on Real might get hijacked. That’s why we want to define this in a local context of sorts.

---

<div class="post-metadata">

### Author: ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)
#### Post date: [February 20, 2023, 6:11am UTC](https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633/25 "2023-02-20T06:11:10Z")

</div>

This came up recently again in [Promotion with rationals when constructing arrays · Issue #849 · JuliaSymbolics/Symbolics.jl · GitHub](https://github.com/JuliaSymbolics/Symbolics.jl/issues/849). The current best practice seems to be

```julia
using Symbolics
sqrt2 = Symbolics.wrap(Symbolics.Term(sqrt, [2]))

```

Please note the additional `Symbolics.wrap`.

---

<div class="post-metadata">

### Author: ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)
#### Post date: [February 16, 2024, 4:51am UTC](https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633/26 "2024-02-16T04:51:45Z")

</div>

Revisiting the problem I wanted to handle with Symbolics.jl, I think I’ve figured out a favourable design for my use case.

Two levels of implementation to enable the flexibility of calling my mathematical functions with both `Num` and actual numeric instances, while avoiding piracy.

```julia
mysqrt(x::Real) = sqrt(x)
mysqrt(x::Num) = Symbolics.Term(sqrt, [x]) |> Symbolics.wrap

```

so when used inside my algorithmic implementations,

```julia
myalg(x::T, y::T) where T <: Real = x / mysqrt(y |> T)

```

will let me call it as both:

```julia
myalg(1, 2)

@variables x, y
myalg(x, y)

```

[Previous page](https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633.md?page=1)
