-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpf5.hs
More file actions
116 lines (79 loc) · 2.79 KB
/
Copy pathpf5.hs
File metadata and controls
116 lines (79 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module PF5 where
any1 :: (a -> Bool) -> [a] -> Bool
any1 f [ ] = False
any1 f (h:t) = f h || any1 f t
zipWith1 :: (a->b->c) -> [a] -> [b]-> [c]
zipWith1 f (x:xs) (y:ys) = f x y : zipWith1 f xs ys
zipWith _ _ _ = [ ]
takeWhile1 :: (a->Bool) -> [a] -> [a]
takeWhile1 f [ ] = [ ]
takeWhile1 f (x:xs)
| f x = x : takeWhile1 f xs
| otherwise = [ ]
dropWhile1 :: (a->Bool) -> [a] -> [a]
dropWhile1 f (x:xs)
| f x = dropWhile1 f xs
| otherwise = x : dropWhile1 f xs
{-
span1 :: (a-> Bool) -> [a] -> ([a],[a])
span1 f (x:xs)
| f x = [x] : span1 f xs
| otherwise = span1 f xs : [x]
-}
deleteBy1 :: (a -> a -> Bool) -> a -> [a] -> [a]
deleteBy1 f x (h:t)
| f x h = t
| otherwise = h : deleteBy1 f x t
sortOn :: Ord b => (a -> b) -> [a] -> [a]
sortOn f [ ] = [ ]
sortOn f (x:xs) = insert f x (sortOn f xs)
insert :: Ord b => (a -> b)-> a -> [a] -> [a]
insert f x [ ] = [x]
insert f x (y:ys)
| f x <= f y = x : y : ys
| otherwise = y : insert f x ys
type Polinomio = [Monomio]
type Monomio = (Float,Int)
conta1 :: Int -> Polinomio -> Int
conta1 n pol = foldr aux1 0 pol
where aux1 (c,e) r = if e == n then 1 + r else r
calcula :: Float -> Polinomio -> Float
calcula n ((x,xs):y) = x*n^xs + calcula n y
calcula1 n pol = foldr (\(c,e) r -> (c*(n^e))+r) 0
--calcula2 :: Float -> Polinomio -> Float
--calcula2 n pol = sum (map (\ x (c,e) -> (c*n^e)) pol)
selgrau :: Int -> Polinomio -> Polinomio
selgrau n pol = filter ((==n) . snd) pol
--selgrau n pol = filter (\x-> (snd x)==n) pol
--selgrau n pol = filter (\(c,e)-> e==n) pol
--selgrau n pol = filter aux pol
-- where aux (c,e) = e == n
deriv :: Polinomio -> Polinomio
deriv pol = foldr (\(c,e) r -> (c*(fromIntegral e), e-1):r) [ ] pol
--deriv pol = map (\ (c,e) -> (c*(fromIntegral e),e -1)) pol
simp :: Polinomio -> Polinomio
simp ((x,y):xs)
| y == 0 = simp xs
| otherwise = (x,y) : simp xs
mult ::Monomio -> Polinomio -> Polinomio
mult (x,xs) ((y,ys):yss) = (x*y, xs+ys) : mult (x,xs) yss
-- ordena :: Polinomio -> Polinomio
-- ordena ((x,y):xs) = isort y snd xs --está mal
--falta a normaliza e a soma
type Mat a = [[a]]
mat1 = [[1,2,3],[0,4,5],[0,0,6]]
dimOK :: Mat a -> Bool
dimOK [ ] = False
dimOK m = let (x:xs) = map length m
in x/=0 && (filter (/=x) xs) == [ ]
addMat :: Num a => Mat a -> Mat a -> Mat a
addMat (la: las) (lb : lbs) = (zipWith1 (+)la lb) : addMat las lbs
addMat [ ] [ ] = [ ]
transpose :: Mat a -> Mat a
transpose ([ ]:t) = [ ]
transpose m = (map head m) : transpose (map tail m)
multMat :: Num a => Mat a -> Mat a -> Mat a
multMat (l:ls) m = (linha l m) : multMat ls m
linha :: Num a => [a]-> Mat a -> [a]
linha l ([]:_) = [ ]
linha l m = sum (zipWith1 (*) l (map head m)) : linha l (map tail m)