Chris@10
|
1 (*
|
Chris@10
|
2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
|
Chris@10
|
3 * Copyright (c) 2003, 2007-11 Matteo Frigo
|
Chris@10
|
4 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
|
Chris@10
|
5 *
|
Chris@10
|
6 * This program is free software; you can redistribute it and/or modify
|
Chris@10
|
7 * it under the terms of the GNU General Public License as published by
|
Chris@10
|
8 * the Free Software Foundation; either version 2 of the License, or
|
Chris@10
|
9 * (at your option) any later version.
|
Chris@10
|
10 *
|
Chris@10
|
11 * This program is distributed in the hope that it will be useful,
|
Chris@10
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@10
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@10
|
14 * GNU General Public License for more details.
|
Chris@10
|
15 *
|
Chris@10
|
16 * You should have received a copy of the GNU General Public License
|
Chris@10
|
17 * along with this program; if not, write to the Free Software
|
Chris@10
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Chris@10
|
19 *
|
Chris@10
|
20 *)
|
Chris@10
|
21
|
Chris@10
|
22 (*
|
Chris@10
|
23 * The LittleSimplifier module implements a subset of the simplifications
|
Chris@10
|
24 * of the AlgSimp module. These simplifications can be executed
|
Chris@10
|
25 * quickly here, while they would take a long time using the heavy
|
Chris@10
|
26 * machinery of AlgSimp.
|
Chris@10
|
27 *
|
Chris@10
|
28 * For example, 0 * x is simplified to 0 tout court by the LittleSimplifier.
|
Chris@10
|
29 * On the other hand, AlgSimp would first simplify x, generating lots
|
Chris@10
|
30 * of common subexpressions, storing them in a table etc, just to
|
Chris@10
|
31 * discard all the work later. Similarly, the LittleSimplifier
|
Chris@10
|
32 * reduces the constant FFT in Rader's algorithm to a constant sequence.
|
Chris@10
|
33 *)
|
Chris@10
|
34
|
Chris@10
|
35 open Expr
|
Chris@10
|
36
|
Chris@10
|
37 let rec makeNum = function
|
Chris@10
|
38 | n -> Num n
|
Chris@10
|
39
|
Chris@10
|
40 and makeUminus = function
|
Chris@10
|
41 | Uminus a -> a
|
Chris@10
|
42 | Num a -> makeNum (Number.negate a)
|
Chris@10
|
43 | a -> Uminus a
|
Chris@10
|
44
|
Chris@10
|
45 and makeTimes = function
|
Chris@10
|
46 | (Num a, Num b) -> makeNum (Number.mul a b)
|
Chris@10
|
47 | (Num a, Times (Num b, c)) -> makeTimes (makeNum (Number.mul a b), c)
|
Chris@10
|
48 | (Num a, b) when Number.is_zero a -> makeNum (Number.zero)
|
Chris@10
|
49 | (Num a, b) when Number.is_one a -> b
|
Chris@10
|
50 | (Num a, b) when Number.is_mone a -> makeUminus b
|
Chris@10
|
51 | (Num a, Uminus b) -> Times (makeUminus (Num a), b)
|
Chris@10
|
52 | (a, (Num b as b')) -> makeTimes (b', a)
|
Chris@10
|
53 | (a, b) -> Times (a, b)
|
Chris@10
|
54
|
Chris@10
|
55 and makePlus l =
|
Chris@10
|
56 let rec reduceSum x = match x with
|
Chris@10
|
57 [] -> []
|
Chris@10
|
58 | [Num a] -> if Number.is_zero a then [] else x
|
Chris@10
|
59 | (Num a) :: (Num b) :: c ->
|
Chris@10
|
60 reduceSum ((makeNum (Number.add a b)) :: c)
|
Chris@10
|
61 | ((Num _) as a') :: b :: c -> b :: reduceSum (a' :: c)
|
Chris@10
|
62 | a :: s -> a :: reduceSum s
|
Chris@10
|
63
|
Chris@10
|
64 in match reduceSum l with
|
Chris@10
|
65 [] -> makeNum (Number.zero)
|
Chris@10
|
66 | [a] -> a
|
Chris@10
|
67 | [a; b] when a == b -> makeTimes (Num Number.two, a)
|
Chris@10
|
68 | [Times (Num a, b); Times (Num c, d)] when b == d ->
|
Chris@10
|
69 makeTimes (makePlus [Num a; Num c], b)
|
Chris@10
|
70 | a -> Plus a
|
Chris@10
|
71
|