-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- # Exclusive scans | ||
-- | ||
-- There are two common ways of specifying [scans](scan-reduce.html): | ||
-- inclusive and exclusive. In an inclusive scan, element *i* of the | ||
-- output includes element *i* of the input, while for an exclusive | ||
-- scan it only goes up to *i-1*. The `scan` functon in Futhark is an | ||
-- inclusive scan, but it is easy to define an exclusive one: | ||
|
||
def exscan f ne xs = | ||
map2 (\i x -> if i == 0 then ne else x) | ||
(indices xs) | ||
(rotate (-1) (scan f ne xs)) | ||
|
||
-- ## See also | ||
-- | ||
-- [Evaluating polynomials](polynomials.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- # Evaluating polynomials | ||
-- | ||
-- A polynomial of the *n*th degree comprises *n* coefficients and can | ||
-- be evaluated at a specific value *x* as follows: | ||
|
||
entry poly as x = | ||
f64.sum (map2 (*) as (map (x**) (map f64.i64 (indices as)))) | ||
|
||
-- [Horner's method](https://en.wikipedia.org/wiki/Horner%27s_method) | ||
-- is a well-known technique for evaluating polynomials using fewer | ||
-- multiplications (in particular, avoiding the use of the power | ||
-- operation). While normally expressed sequentially, it can also be | ||
-- implemented in Futhark using an [exclusive | ||
-- scan](exclusive-scan.html): | ||
|
||
import "exclusive-scan" | ||
|
||
entry horner as x = | ||
f64.sum (map2 (*) as (exscan (*) 1 (map (const x) as))) | ||
|
||
-- In most cases, the additional overhead of manifesting the result of | ||
-- the `scan` exceeds the savings from not evaluating `**`, so `poly` | ||
-- is often fastest. |