<commit_before>{-|
    This module implements the multiplication algorithm presented in:
    [BT97] http://dx.doi.org/10.1016/0024-3795(95)00696-6

-}
module FnReps.Polynomial.UnaryChebSparse.DCTMultiplication 
--(multiplyDCT_terms)
where

--import Numeric.AERN.MPFRBasis.Interval
import Numeric.AERN.DoubleBasis.Interval
import Numeric.AERN.RealArithmetic.RefinementOrderRounding ((/|), (|*))

import qualified Data.HashMap.Strict as HM

--import Debug.Trace (trace)

--type RA = MI
type RA = DI

multiplyDCT_terms :: HM.HashMap Int RA -> HM.HashMap Int RA -> HM.HashMap Int RA
multiplyDCT_terms =
    undefined

{-|
    Simplified DCT-III computed directly from its definition in
    [BT97, page 20, display (6.3)].
    
    This is quite inefficient.  It is to be used only as a reference in tests.
-}
tSDCT_III_reference :: 
    [RA] {-^ h a vector of validated real numbers -} -> 
    [RA] {-^ h~ a vector of validated real numbers -}
tSDCT_III_reference h =
--    trace (
--        "rPi = " ++ show rPi
--        ++ "\ncN = " ++ show cN
--        ++ "\ncN1 = " ++ show cN1
--        ++ "\n( (((4*3+1)*3 :: Int) |* rPi) /| cN) = " ++ show (( (((4*3+1)*3 :: Int) |* rPi) /| cN))
--        ++ "\ncos ( (((4*3+1)*3 :: Int) |* rPi) /| cN) = " ++ show (cos ( (((4*3+1)*3 :: Int) |* rPi) /| cN))
--    ) $
    [sum [ (h !! ell) * cos ( (((4*j+1)*ell) |* rPi) /| cN)
            | ell <- [0..(cN1-1)] 
         ] 
        | j <- [0..(cN1-1)]
    ]
    where
    cN = cN1 * 2
    cN1 = length h
    rPi = piOut -- rSample
    (rSample : _) = h

{-|
    Simplified DCT-III computed as described in 
    [BT97, page 21, Algorithm 1].
    
    Changed many occurrences of N1 with N1-1 because the indices were out of range.
    This is part of a trial and error process.
    
    Precondition: length h is a power of 2
-}
tSDCT_III_nlogn :: 
    [RA] {-^ h a vector of validated real numbers -} -> 
    [RA] {-^ h~ a vector of validated real numbers -}
tSDCT_III_nlogn h =
    map (\ (_,[a],_) -> a) $ splitUntilSingletons $ [(0, h, 1)]
    where
    splitUntilSingletons :: [(Int, [RA], Int)] -> [(Int, [RA], Int)]
    splitUntilSingletons groups<commit_msg>Fixed the order of the elements in the vector returned by SDCT-III. This function now passed several hand-made tests.<commit_after>{-|
    This module implements the multiplication algorithm presented in:
    [BT97] http://dx.doi.org/10.1016/0024-3795(95)00696-6

-}
module FnReps.Polynomial.UnaryChebSparse.DCTMultiplication 
--(multiplyDCT_terms)
where

--import Numeric.AERN.MPFRBasis.Interval
import Numeric.AERN.DoubleBasis.Interval
import Numeric.AERN.RealArithmetic.RefinementOrderRounding ((/|), (|*))

import qualified Data.HashMap.Strict as HM
import Data.List (sortBy)

--import Debug.Trace (trace)

--type RA = MI
type RA = DI

multiplyDCT_terms :: HM.HashMap Int RA -> HM.HashMap Int RA -> HM.HashMap Int RA
multiplyDCT_terms =
    undefined

{-|
    Simplified DCT-III computed directly from its definition in
    [BT97, page 20, display (6.3)].
    
    This is quite inefficient.  It is to be used only as a reference in tests.
-}
tSDCT_III_reference :: 
    [RA] {-^ h a vector of validated real numbers -} -> 
    [RA] {-^ h~ a vector of validated real numbers -}
tSDCT_III_reference h =
--    trace (
--        "rPi = " ++ show rPi
--        ++ "\ncN = " ++ show cN
--        ++ "\ncN1 = " ++ show cN1
--        ++ "\n( (((4*3+1)*3 :: Int) |* rPi) /| cN) = " ++ show (( (((4*3+1)*3 :: Int) |* rPi) /| cN))
--        ++ "\ncos ( (((4*3+1)*3 :: Int) |* rPi) /| cN) = " ++ show (cos ( (((4*3+1)*3 :: Int) |* rPi) /| cN))
--    ) $
    [sum [ (h !! ell) * cos ( (((4*j+1)*ell) |* rPi) /| cN)
            | ell <- [0..(cN1-1)] 
         ] 
        | j <- [0..(cN1-1)]
    ]
    where
    cN = cN1 * 2
    cN1 = length h
    rPi = piOut -- rSample
    (rSample : _) = h

{-|
    Simplified DCT-III computed as described in 
    [BT97, page 21, Algorithm 1].
    
    Changed many occurrences of N1 with N1-1 because the indices were out of range.
    This is part of a trial and error process.
    
    Precondition: length h is a power of 2
-}
tSDCT_III_nlogn :: 
    [RA] {-^ h a vector of validated real numbers -} -> 
    [RA] {-^ h~ a vector of validated real numbers -}
tSDCT_III_nlogn h =
    map (\ (_,[a],_) -> a) $
        sortBy (\ (i,_,_) (j,_,_) -> compare i j) $ 
        splitUntilSingletons $ [(0, h, 1)]
    where
    splitUntilSingletons :: [(Int, [RA], Int)] -> [(Int, [RA], Int)]
    splitUntilSingletons groups
{'fraction_non_alphanumeric': 0.16611074049366245, 'fraction_numerical': 0.0311318656882366, 'mean_word_length': 2.430968726163234, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4556480', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1648, 'n_words': 478}