You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package exports the type `PiExpTimes{N,T}` that satisfies `PiExpTimes{N,T}(x) = x*π^N`, and the type `PiTimes` that is aliased to `PiExpTimes{1}`. It also provides the constant `Pi` for convenience, defined as `PiTimes(1)`, that behaves like `π` except it produces results with higher accuracy in certain trigonometric and algebraic contexts. In most scenarios the numbers `Pi` and `π` are interchangable.
9
+
This package exports the type `PiExpTimes` that satisfies `PiExpTimes(x, n) = x*π^n`. It also provides the constant `Pi` for convenience, defined as `PiExpTimes(1, 1)`, that behaves like `π` except it produces results with higher accuracy in certain trigonometric and algebraic contexts. In most scenarios the numbers `Pi` and `π` are interchangable.
11
10
12
11
```julia
13
12
julia> Pi^2== π^2
@@ -24,7 +23,7 @@ The number `π` is represented as an `Irrational` type in julia, and may be comp
24
23
25
24
## Arithmetic
26
25
27
-
Delaying the conversion of `π` to a float results in satisfying mathematical expressions such as
26
+
Delaying the conversion of `π` to a float results in precise mathematical expressions such as
The type `PiExpTimes{N}` stores the exponent as a type-parameter, therefore exponentiation is not type-stable in general.
141
-
142
-
### Constructor-abuse to avoid nesting
143
-
144
-
`PiExpTimes{N}(PiExpTimes{M})` is automatically simplified to `PiExpTimes{N+M}`. This is an abuse of Julia's constructors as the type of the object changes, however this avoids nested expressions that have performance issues.
145
-
146
-
```julia
147
-
julia>PiExpTimes{2}(PiExpTimes{3}(4))
148
-
4Pi^5
149
-
150
-
julia>PiExpTimes{2}(PiExpTimes{3}(4)) |> typeof
151
-
PiExpTimes{5,Int64}
152
-
```
153
-
154
109
### Interactions with π
155
110
156
-
The irrational number `π` is usually aggressively converted to `PiTimes(1)`, eg:
111
+
The irrational number `π` is usually aggressively converted to `Pi`, eg:
157
112
158
113
```julia
159
-
julia>PiTimes(π)
114
+
julia>π * Pi
160
115
Pi^2
161
116
```
162
117
163
-
This ensures that subsequent calculation would not get promoted to a floating-point type. However if this behavior is not desired then one may specify the type explicitly while constructing the object as
118
+
This ensures that subsequent calculation would not get promoted to a floating-point type. However if this behavior is not desired then one may specify the type explicitly while constructing the object as
164
119
165
120
```julia
166
-
julia>PiTimes{Irrational{:π}}(π)
167
-
π=3.1415926535897...*Pi
121
+
julia>PiExpTimes{Irrational{:π}}(π)
122
+
π*Pi^0
168
123
```
169
124
170
-
However, it is not possible to convert a number to the type `PiTimes{Irrational{:π}}`.
171
-
172
125
### Floating-point promotion
173
126
174
127
Addition and subtraction involving mixed exponents of `Pi` will involve floating-point conversions, and the resulting expression will have the minimum exponent out of the terms being summed.
@@ -183,89 +136,16 @@ julia> Pi + 3Pi^2
183
136
184
137
This fits with the intuition of the expression being factorized as `Pi + 3Pi^2 == Pi*(1 + 3Pi)`.
185
138
186
-
Note that `π` is promoted to `Pi` in such operations, so we obtain
139
+
Note that `π` is promoted to `Pi` in such operations, so we obtain
187
140
188
141
```julia
189
142
julia> Pi + π
190
143
2Pi
191
144
```
192
145
193
-
### Conversion vs construction
194
-
195
-
Constructors for `PiExpTimes` act as a wrapper and not as a conversion. Conversion to the type retains the value of a number, whereas construction implies multiplication by an exponent of `Pi`.
196
-
197
-
```julia
198
-
julia>PiTimes(1)
199
-
Pi
200
-
201
-
julia>convert(PiTimes,1)
202
-
Pi^-1*Pi
203
-
```
204
-
205
-
### Promotion of mixed types
206
-
207
-
`promote` and `promote_type` work differently with types having different exponents. `promote` converts both the types to one that has the minimum exponent, whereas `promote_type` leaves the exponent as a free parameter.
208
-
209
-
```julia
210
-
julia>promote(Pi,Pi^2) |> typeof
211
-
Tuple{PiExpTimes{1,Real},PiExpTimes{1,Real}}
212
-
213
-
julia>promote_type(typeof(Pi),typeof(Pi^2))
214
-
PiExpTimes{N,Int64} where N
215
-
```
216
-
217
-
This is so that structs of `PiTimes`— such as complex numbers — do not lose accuracy in conversion. The behaviour is explained below with some examples:
218
-
219
-
#### Arrays of mixed types
220
-
221
-
Storing different exponents of `Pi` in an array will in general lead to conversion to a supertype that can store all the values.
222
-
223
-
```julia
224
-
julia> [Pi,Pi^2] # element type is not concrete
225
-
2-element Array{PiExpTimes{N,Int64} where N,1}:
226
-
Pi
227
-
Pi^2
228
-
```
229
-
230
-
Such an array will not be the most performant, as the element type is not concrete. This may be avoided by specifying a type while creating the array. A concrete type will not be able to store all the numbers losslessly.
231
-
232
-
```julia
233
-
julia> PiExpTimes{2,Real}[Pi,Pi^2] # exponent is fixed but Real is not a concrete type
234
-
2-element Array{PiExpTimes{2,Real},1}:
235
-
Pi^-1*Pi^2
236
-
Pi^2
237
-
238
-
julia> PiExpTimes{2,Float64}[Pi,Pi^2] # eltype is concrete, but result loses accuracy
239
-
2-element Array{PiExpTimes{2,Float64},1}:
240
-
0.3183098861837907*Pi^2
241
-
Pi^2
242
-
```
243
-
244
-
#### Complex numbers
245
-
246
-
Complex numbers rely on `promote` to generate the element type
247
-
248
-
```julia
249
-
julia>Complex(Pi,Pi^2)
250
-
Pi + Pi*Pi*im
251
-
252
-
julia>Complex(Pi,Pi^2) |> typeof
253
-
Complex{PiExpTimes{1,Real}}
254
-
```
255
-
256
-
In this case converting either the real or imaginary part to a floating-point type would have resulted in a loss of accuracy. Such a type might not be performant, so if a conversion is desired then it might be enforced by specifying the element type while constructing the `Complex` struct:
0 commit comments