Chris@19: (* Chris@19: * Copyright (c) 1997-1999 Massachusetts Institute of Technology Chris@19: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@19: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@19: * Chris@19: * This program is free software; you can redistribute it and/or modify Chris@19: * it under the terms of the GNU General Public License as published by Chris@19: * the Free Software Foundation; either version 2 of the License, or Chris@19: * (at your option) any later version. Chris@19: * Chris@19: * This program is distributed in the hope that it will be useful, Chris@19: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@19: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@19: * GNU General Public License for more details. Chris@19: * Chris@19: * You should have received a copy of the GNU General Public License Chris@19: * along with this program; if not, write to the Free Software Chris@19: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@19: * Chris@19: *) Chris@19: Chris@19: (* Chris@19: * The LittleSimplifier module implements a subset of the simplifications Chris@19: * of the AlgSimp module. These simplifications can be executed Chris@19: * quickly here, while they would take a long time using the heavy Chris@19: * machinery of AlgSimp. Chris@19: * Chris@19: * For example, 0 * x is simplified to 0 tout court by the LittleSimplifier. Chris@19: * On the other hand, AlgSimp would first simplify x, generating lots Chris@19: * of common subexpressions, storing them in a table etc, just to Chris@19: * discard all the work later. Similarly, the LittleSimplifier Chris@19: * reduces the constant FFT in Rader's algorithm to a constant sequence. Chris@19: *) Chris@19: Chris@19: open Expr Chris@19: Chris@19: let rec makeNum = function Chris@19: | n -> Num n Chris@19: Chris@19: and makeUminus = function Chris@19: | Uminus a -> a Chris@19: | Num a -> makeNum (Number.negate a) Chris@19: | a -> Uminus a Chris@19: Chris@19: and makeTimes = function Chris@19: | (Num a, Num b) -> makeNum (Number.mul a b) Chris@19: | (Num a, Times (Num b, c)) -> makeTimes (makeNum (Number.mul a b), c) Chris@19: | (Num a, b) when Number.is_zero a -> makeNum (Number.zero) Chris@19: | (Num a, b) when Number.is_one a -> b Chris@19: | (Num a, b) when Number.is_mone a -> makeUminus b Chris@19: | (Num a, Uminus b) -> Times (makeUminus (Num a), b) Chris@19: | (a, (Num b as b')) -> makeTimes (b', a) Chris@19: | (a, b) -> Times (a, b) Chris@19: Chris@19: and makePlus l = Chris@19: let rec reduceSum x = match x with Chris@19: [] -> [] Chris@19: | [Num a] -> if Number.is_zero a then [] else x Chris@19: | (Num a) :: (Num b) :: c -> Chris@19: reduceSum ((makeNum (Number.add a b)) :: c) Chris@19: | ((Num _) as a') :: b :: c -> b :: reduceSum (a' :: c) Chris@19: | a :: s -> a :: reduceSum s Chris@19: Chris@19: in match reduceSum l with Chris@19: [] -> makeNum (Number.zero) Chris@19: | [a] -> a Chris@19: | [a; b] when a == b -> makeTimes (Num Number.two, a) Chris@19: | [Times (Num a, b); Times (Num c, d)] when b == d -> Chris@19: makeTimes (makePlus [Num a; Num c], b) Chris@19: | a -> Plus a Chris@19: