Skip to content

Commit f062329

Browse files
committed
Add Floating instance.
1 parent b7940b7 commit f062329

File tree

5 files changed

+79
-76
lines changed

5 files changed

+79
-76
lines changed

src/ArrayFire.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ import Data.Word
191191
-- Specifying up to 4 dimensions is allowed (anything high is ignored).
192192

193193
-- $laws
194-
-- Every 'Array' is an instance of 'Eq', 'Ord', 'Num', 'Fractional' and 'SemiGroup'
194+
-- Every 'Array' is an instance of 'Eq', 'Ord', 'Num', 'Fractional', 'Floating' and 'SemiGroup'
195195
--
196196
-- 'Num'
197197
--

src/ArrayFire/Arith.hs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,49 @@ tanh
12781278
-- ^ Result of calling 'tanh'
12791279
tanh = flip op1 af_tanh
12801280

1281+
-- | Execute asinh
1282+
--
1283+
-- >>> A.asinh (A.vector @Int 10 [1..])
1284+
-- ArrayFire Array
1285+
-- [10 1 1 1]
1286+
-- 0.7616 0.9640 0.9951 0.9993 0.9999 1.0000 1.0000 1.0000 1.0000 1.0000
1287+
asinh
1288+
:: AFType a
1289+
=> Array a
1290+
-- ^ Input array
1291+
-> Array a
1292+
-- ^ Result of calling 'tanh'
1293+
asinh = flip op1 af_asinh
1294+
1295+
-- | Execute acosh
1296+
--
1297+
-- >>> A.acosh (A.vector @Int 10 [1..])
1298+
-- ArrayFire Array
1299+
-- [10 1 1 1]
1300+
-- 0.7616 0.9640 0.9951 0.9993 0.9999 1.0000 1.0000 1.0000 1.0000 1.0000
1301+
acosh
1302+
:: AFType a
1303+
=> Array a
1304+
-- ^ Input array
1305+
-> Array a
1306+
-- ^ Result of calling 'tanh'
1307+
acosh = flip op1 af_acosh
1308+
1309+
-- | Execute atanh
1310+
--
1311+
-- >>> A.atanh (A.vector @Int 10 [1..])
1312+
-- ArrayFire Array
1313+
-- [10 1 1 1]
1314+
-- 0.7616 0.9640 0.9951 0.9993 0.9999 1.0000 1.0000 1.0000 1.0000 1.0000
1315+
atanh
1316+
:: AFType a
1317+
=> Array a
1318+
-- ^ Input array
1319+
-> Array a
1320+
-- ^ Result of calling 'tanh'
1321+
atanh = flip op1 af_atanh
1322+
1323+
12811324
-- | Execute root
12821325
--
12831326
-- >>> A.root (A.vector @Double 10 [1..]) (A.vector @Double 10 [1..])

src/ArrayFire/Device.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import ArrayFire.Internal.Device
2424
import ArrayFire.FFI
2525

2626
-- | Retrieve info from ArrayFire API
27+
-- @
28+
-- ArrayFire v3.6.4 (OpenCL, 64-bit Mac OSX, build 1b8030c5)
29+
-- [0] APPLE: AMD Radeon Pro 555X Compute Engine, 4096 MB
30+
-- -1- APPLE: Intel(R) UHD Graphics 630, 1536 MB
31+
-- @
2732
info :: IO ()
2833
info = afCall af_info
2934

src/ArrayFire/Orphans.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,18 @@ instance Show (Array a) where
5454
instance forall a . (Fractional a, AFType a) => Fractional (Array a) where
5555
x / y = A.div x y
5656
fromRational n = A.scalar @a (fromRational n)
57+
58+
instance forall a . (Ord a, AFType a, Fractional a) => Floating (Array a) where
59+
pi = A.scalar @a 3.14159
60+
exp = A.exp @a
61+
log = A.log @a
62+
sin = A.sin @a
63+
cos = A.cos @a
64+
asin = A.asin @a
65+
acos = A.acos @a
66+
atan = A.atan @a
67+
sinh = A.sinh @a
68+
cosh = A.cosh @a
69+
acosh = A.acosh @a
70+
atanh = A.atanh @a
71+
asinh = A.asinh @a

src/ArrayFire/Statistics.hs

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@ import ArrayFire.Internal.Types
3737

3838
-- | Calculates 'mean' of 'Array' along user-specified dimension.
3939
--
40-
-- @
41-
-- >>> print $ mean 0 ( vector @Int 10 [1..] )
42-
-- @
43-
-- @
40+
-- >>> mean 0 ( vector @Int 10 [1..] )
4441
-- ArrayFire Array
4542
-- [1 1 1 1]
4643
-- 5.5000
47-
-- @
4844
mean
4945
:: AFType a
5046
=> Array a
@@ -59,14 +55,10 @@ mean a n =
5955

6056
-- | Calculates 'meanWeighted' of 'Array' along user-specified dimension.
6157
--
62-
-- @
63-
-- >>> print $ meanWeighted (vector @Double 10 [1..10]) (vector @Double 10 [1..10]) 0
64-
-- @
65-
-- @
58+
-- >>> meanWeighted (vector @Double 10 [1..10]) (vector @Double 10 [1..10]) 0
6659
-- ArrayFire Array
6760
-- [1 1 1 1]
6861
-- 7.0000
69-
-- @
7062
meanWeighted
7163
:: AFType a
7264
=> Array a
@@ -83,14 +75,10 @@ meanWeighted x y (fromIntegral -> n) =
8375

8476
-- | Calculates 'variance' of 'Array' along user-specified dimension.
8577
--
86-
-- @
87-
-- >>> print $ var (vector @Double 8 [1..8]) False 0
88-
-- @
89-
-- @
78+
-- >>> var (vector @Double 8 [1..8]) False 0
9079
-- ArrayFire Array
9180
-- [1 1 1 1]
9281
-- 6.0
93-
-- @
9482
var
9583
:: AFType a
9684
=> Array a
@@ -107,14 +95,10 @@ var arr (fromIntegral . fromEnum -> b) d =
10795

10896
-- | Calculates 'varWeighted' of 'Array' along user-specified dimension.
10997
--
110-
-- @
111-
-- >>> print $ varWeighted 0 ( vector @Int 10 [1..] ) ( vector @Int 10 [1..] )
112-
-- @
113-
-- @
98+
-- >>> varWeighted 0 ( vector @Int 10 [1..] ) ( vector @Int 10 [1..] )
11499
-- ArrayFire Array
115100
-- [1 1 1 1]
116101
-- 5.5000
117-
-- @
118102
varWeighted
119103
:: AFType a
120104
=> Array a
@@ -131,14 +115,10 @@ varWeighted x y (fromIntegral -> n) =
131115

132116
-- | Calculates 'stdev' of 'Array' along user-specified dimension.
133117
--
134-
-- @
135118
-- >>> stdev (vector @Double 10 (cycle [1,-1])) 0
136-
-- @
137-
-- @
138119
-- ArrayFire Array
139120
-- [1 1 1 1]
140121
-- 1.0
141-
-- @
142122
stdev
143123
:: AFType a
144124
=> Array a
@@ -153,14 +133,10 @@ stdev a n =
153133

154134
-- | Calculates 'covariance' two 'Array's with a bias specifier.
155135
--
156-
-- @
157-
-- >>> print $ cov (vector @Double 10 (repeat 1)) (vector @Double 10 (repeat 1)) False
158-
-- @
159-
-- @
136+
-- >>> cov (vector @Double 10 (repeat 1)) (vector @Double 10 (repeat 1)) False
160137
-- ArrayFire Array
161138
-- [1 1 1 1]
162139
-- 0.0
163-
-- @
164140
cov
165141
:: AFType a
166142
=> Array a
@@ -177,14 +153,10 @@ cov x y (fromIntegral . fromEnum -> n) =
177153

178154
-- | Calculates 'median' of 'Array' along user-specified dimension.
179155
--
180-
-- @
181156
-- >>> print $ median ( vector @Int 10 [1..] ) 0
182-
-- @
183-
-- @
184157
-- ArrayFire Array
185158
-- [1 1 1 1]
186159
-- 5.5000
187-
-- @
188160
median
189161
:: AFType a
190162
=> Array a
@@ -199,12 +171,8 @@ median a n =
199171

200172
-- | Calculates 'mean' of all elements in an 'Array'
201173
--
202-
-- @
203-
-- >>> print $ fst (meanAll (matrix @Double (2,2) (repeat 10)))
204-
-- @
205-
-- @
206-
-- >>> 10.0
207-
-- @
174+
-- >>> fst (meanAll (matrix @Double (2,2) (repeat 10)))
175+
-- 10.0
208176
meanAll
209177
:: AFType a
210178
=> Array a
@@ -215,12 +183,8 @@ meanAll = (`infoFromArray2` af_mean_all)
215183

216184
-- | Calculates weighted mean of all elements in an 'Array'
217185
--
218-
-- @
219186
-- >>> print $ fst (meanAllWeighted (matrix @Double (2,2) (repeat 10)) (matrix @Double (2,2) (repeat 0)))
220-
-- @
221-
-- @
222187
-- 10
223-
-- @
224188
meanAllWeighted
225189
:: AFType a
226190
=> Array a
@@ -234,12 +198,8 @@ meanAllWeighted a b =
234198

235199
-- | Calculates variance of all elements in an 'Array'
236200
--
237-
-- @
238-
-- >>> print $ fst (varAll (vector @Double 10 (repeat 10)) False)
239-
-- @
240-
-- @
201+
-- >>> fst (varAll (vector @Double 10 (repeat 10)) False)
241202
-- 0
242-
-- @
243203
varAll
244204
:: AFType a
245205
=> Array a
@@ -254,12 +214,8 @@ varAll a (fromIntegral . fromEnum -> b) =
254214

255215
-- | Calculates weighted variance of all elements in an 'Array'
256216
--
257-
-- @
258-
-- >>> print $ varAllWeighted ( vector @Int 10 [1..] ) ( vector @Int 10 [1..] )
259-
-- @
260-
-- @
217+
-- >>> varAllWeighted ( vector @Int 10 [1..] ) ( vector @Int 10 [1..] )
261218
-- 0
262-
-- @
263219
varAllWeighted
264220
:: AFType a
265221
=> Array a
@@ -273,12 +229,8 @@ varAllWeighted a b =
273229

274230
-- | Calculates standard deviation of all elements in an 'Array'
275231
--
276-
-- @
277-
-- >>> print $ fst (stdevAll (vector @Double 10 (repeat 10)))
278-
-- @
279-
-- @
232+
-- >>> fst (stdevAll (vector @Double 10 (repeat 10)))
280233
-- 10
281-
-- @
282234
stdevAll
283235
:: AFType a
284236
=> Array a
@@ -289,12 +241,8 @@ stdevAll = (`infoFromArray2` af_stdev_all)
289241

290242
-- | Calculates median of all elements in an 'Array'
291243
--
292-
-- @
293-
-- >>> print $ fst (medianAll (vector @Double 10 (repeat 10)))
294-
-- @
295-
-- @
244+
-- >>> fst (medianAll (vector @Double 10 (repeat 10)))
296245
-- 10
297-
-- @
298246
medianAll
299247
:: (AFType a, Fractional a)
300248
=> Array a
@@ -306,12 +254,8 @@ medianAll = (`infoFromArray2` af_median_all)
306254
-- | This algorithm returns Pearson product-moment correlation coefficient.
307255
-- <https://en.wikipedia.org/wiki/Pearson_correlation_coefficient>
308256
--
309-
-- @
310-
-- >>> print $ fst (corrCoef ( vector @Int 10 [1..] ) ( vector @Int 10 [10,9..] ))
311-
-- @
312-
-- @
257+
-- >>> fst (corrCoef ( vector @Int 10 [1..] ) ( vector @Int 10 [10,9..] ))
313258
-- -1
314-
-- @
315259
corrCoef
316260
:: AFType a
317261
=> Array a
@@ -325,21 +269,17 @@ corrCoef a b =
325269

326270
-- | This function returns the top k values along a given dimension of the input array.
327271
--
328-
-- @
329272
-- >>> let (vals,indexes) = 'topk' ( 'vector' \@'Double' 10 [1..] ) 3 'TopKDefault'
330-
-- >>> print vals
331273
-- >>> print indexes
332-
-- @
333-
-- @
274+
--
334275
-- ArrayFire 'Array'
335276
-- [3 1 1 1]
336277
-- 10.0000 9.0000 8.0000
337-
-- @
338-
-- @
278+
--
279+
-- >>> print vals
339280
-- ArrayFire 'Array'
340281
-- [3 1 1 1]
341282
-- 9 8 7
342-
-- @
343283
--
344284
-- The indices along with their values are returned. If the input is a multi-dimensional array, the indices will be the index of the value in that dimension. Order of duplicate values are not preserved. This function is optimized for small values of k.
345285
-- This function performs the operation across all dimensions of the input array.

0 commit comments

Comments
 (0)