# Abstract supertype of AbstractAlgebra.jl polynomials and Floats

**URL:** <https://discourse.julialang.org/t/abstract-supertype-of-abstractalgebra-jl-polynomials-and-floats/51958>\
**Category:** General Usage\
**Tags:** type, symbolic\
**Created:** [December 17, 2020, 3:27am UTC](https://discourse.julialang.org/t/abstract-supertype-of-abstractalgebra-jl-polynomials-and-floats/51958 "2020-12-17T03:27:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [December 17, 2020, 3:27am UTC](https://discourse.julialang.org/t/abstract-supertype-of-abstractalgebra-jl-polynomials-and-floats/51958/1 "2020-12-17T03:27:17Z")

</div>

I have a struct that contains a real number describing some probability. The algorithm that uses this struct performs fairly simple arithmetic that I would like to be able to track both numerically (Floats) and symbolically (e.g. by using AbstractAlgebra.jl).

Initially I started with

```julia
struct MyPhysicsModel
    prob::Float64
end

```

which was then updated to

```julia
struct MyPhysicsModel{T<:Real}
    prob::T
end

```

At this point I am able to use Float64, BigFloat, etc. However what about having a probability which is a symbolic expression. I want to be able to do

```julia
R, (e,) = PolynomialRing(RealField, ["e"])
model = MyPhysicsModel(e)

```

However, this does not work because `typeof(e)` is `AbstractAlgebra.Generic.MPoly{BigFloat}` which is not a subtype of `Real`.

Any idea how to proceed such that `MyModel` can support both numeric reals and symbolic polynomials?

---

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [December 17, 2020, 4:47am UTC](https://discourse.julialang.org/t/abstract-supertype-of-abstractalgebra-jl-polynomials-and-floats/51958/2 "2020-12-17T04:47:06Z")

</div>

> [@Krastanov](#):
>
> ```julia
> struct MyPhysicsModel{T<:Real}
> prob::T
> end
> 
> ```

Just drop the `<:Real`

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [December 17, 2020, 4:58am UTC](https://discourse.julialang.org/t/abstract-supertype-of-abstractalgebra-jl-polynomials-and-floats/51958/3 "2020-12-17T04:58:33Z")

</div>

oh… That sounds kinda obvious now that you said it. Would I suffer any performance or compilation penalties?

---

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [December 17, 2020, 5:20am UTC](https://discourse.julialang.org/t/abstract-supertype-of-abstractalgebra-jl-polynomials-and-floats/51958/4 "2020-12-17T05:20:14Z")

</div>

My understanding is that any methods that use `MyPhysicsModel` will be compiled for precisely the concrete types that you actually use. So if, for example you have a `MyPhysicsModel{Float64}`, the compiler has access to any benefit of realness whether or not you’ve annotated with `<:Real`. The polynomial version will be slower than the float version, but still as individually fast as possible, and could still end up faster than something like `Rational{BigInt}` which is a subtype of `Real`.

The gotcha’s won’t start popping up until you start making things like mixed containers, say an `Array` or `Dict` that holds some `Float` and some `Polynomial` objects, but again, this happens whether or not you’ve forced yourself to subtype `Real`.

You’d also open the possibility of someone making a `MyPhysicsModel{IO}` or of some other completely ludicrous type, but it seems unlikely that said person would be surprised by the resulting errors.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [December 17, 2020, 12:15pm UTC](https://discourse.julialang.org/t/abstract-supertype-of-abstractalgebra-jl-polynomials-and-floats/51958/5 "2020-12-17T12:15:52Z")

</div>

No 🙂
