









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A mathematica notebook from a university course on functional programming, where students define complex functions using compositions and apply them to lists. The document also includes the implementation of a histogram plotting function and its application to different data sets. The goal is to understand the concepts of functional programming, list manipulation, and data visualization.
Typology: Lab Reports
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Clear[y]; y = Sqrt[4]
2
Clear[z]; z = Sqrt[x] è!!!! x
z è!!!! x
Clear[z];
z
z
??Sqrt
Attributes@SqrtD = 8 Listable, NumericFunction, Protected<
ü Function Composition
Sqrt[Sqrt[16]]
2
User-Defined Functions
ü Pure Functions
Function @ z, z^2 D
Function@z, z^2 D
??Function
Function@bodyD or body& is a pure function. The formal parameters are # Hor #1L, #2, etc. Function@x, bodyD is a pure function with a single formal parameter x. Function@
Attributes@FunctionD = 8 HoldAll, Protected<
Function @ z, z^2 D@ 5 D
25
Attributes@SlotD = 8 NHoldAll, Protected<
ü Defining Complex Functions
compFunc[x_] := Sqrt[Sqrt[x]]
compFunc[16]
2
Function @ z, z^2 D@8 1, 2, 3 Application <> ".txt" L
8 1, 1, 3, 2, 1, 4, 1, 2, 2, 3, 2, 2, 1, 1, 2, 3, 1, 2, 1, 2, 4, 3, 2, 1, 2, 5, 2, 1, 4, 1, 2, 5, 2, 2, 4, 1, 3, 1, 1, 2, 1, 1, 2, 3, 3, 2, 2, 3, 1, 1, 5, 3, 1, 5, 1, 4, 2, 5, 3, 1, 2, 2, 1, 1, 3, 3, 1, 3, 4, 4, 1, 3, 4, 2, 2, 3, 2, 5, 1, 2, 2, 3, 2, 2, 1, 1, 3, 1, 3, 2, 3, 1, 1, 3, 1, 1, 1, 4, 3, 1, 3, 1, 2, 2, 3, 1, 1, 4, 5, 2, 2, 2, 2, 2, 1, 2, 4, 5, 2, 1, 1, 1, 4, 1, 5, 4, 2, 3, 4, 4, 1, 1, 3, 3, 3, 2, 1, 2, 2, 4, 3, 1, 1, 1, 2, 1, 2, 5, 1, 3, 3, 1, 1, 3, 3, 1, 2, 1, 2, 2, 3, 2, 1, 3, 2, 5, 1, 4, 5, 1, 2, 3, 2, 4, 1, 2, 2, 1, 4, 4, 1, 2, 1, 2, 1, 5, 3, 5, 1, 2, 4, 3, 1, 2, 4, 1, 3, 2, 1, 2<
depths = Union @ depthArray D
8 1, 2, 3, 4, 5<
totalPerDepth = Map @ Length @ Cases @ depthArray, # DD &, depths D
8 67, 59, 38, 22, 14<
Apply @ Plus, totalPerDepth D
200
**< Smoke Tests
smokeMatrix = << H "Smoke_Terp" <> Application <> ".txt" L ;
Length[smokeMatrix]
6782
Length[Transpose[smokeMatrix]]
200
Transpose[smokeMatrix][[1]]
8 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 2, 2, 1, 2, 1, 0, 2, 1, 1, 0, 1, 1, 3, 1, 1, 0, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 0, 1<
??Position
Position@expr, patternD gives a list of the positions at which objects matching pattern appear in expr. Position@expr, pattern, levspecD finds only objects that appear on levels specified by levspec. Position@expr,
Attributes@PositionD = 8 Protected<
Options@PositionD = 8 Heads → True<
zeros = Position[Map[Apply[Plus, #]&, Transpose[smokeMatrix]], 0]
883 <, 86 <, 812 <, 816 <, 829 <, 832 <, 834 <, 835 <, 852 <, 872 <, 878 <, 891 <, 8104 <, 8105 <, 8107 <, 8109 <, 8114 <, 8117 <, 8118 <, 8126 <, 8129 <, 8134 <, 8140 <, 8157 <, 8166 <, 8168 <, 8169 <, 8175 <, 8179 <, 8185 <, 8199 <<
Range[Length[Transpose[smokeMatrix]]]
8 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, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200<
??Delete
Delete@expr, nD deletes the element at position n in expr. If n is negative, the position is counted from the end. Delete@expr, 8 i, j, ... smokeFaultsFound = Delete[Range[Length[Transpose[smokeMatrix]]], zeros]
8 1, 2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 33, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 106, 108, 110, 111, 112, 113, 115, 116, 119, 120, 121, 122, 123, 124, 125, 127, 128, 130, 131, 132, 133, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 158, 159, 160, 161, 162, 163, 164, 165, 167, 170, 171, 172, 173, 174, 176, 177, 178, 180, 181, 182, 183, 184, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 200<
Length[Delete[Range[Length[Transpose[smokeMatrix]]], zeros]]
169
smokeDepths = Map @8 1, # < &, depthArray @@ smokeFaultsFound DDD
88 1, 1<, 8 1, 1<, 8 1, 2<, 8 1, 1<, 8 1, 1<, 8 1, 2<, 8 1, 2<, 8 1, 3<, 8 1, 2<, 8 1, 1<, 8 1, 1<, 8 1, 2<, 8 1, 1<, 8 1, 2<, 8 1, 1<, 8 1, 2<, 8 1, 4<, 8 1, 3<, 8 1, 2<, 8 1, 1<, 8 1, 2<, 8 1, 5<, 8 1, 2<, 8 1, 1<, 8 1, 1<, 8 1, 2<, 8 1, 2<, 8 1, 1<, 8 1, 3<, 8 1, 1<, 8 1, 1<, 8 1, 2<, 8 1, 1<, 8 1, 1<, 8 1, 2<, 8 1, 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, 1<, 8 1, 4<, 8 1, 1<, 8 1, 2<, 8 1, 1<, 8 1, 2<, 8 1, 5<, 8 1, 3<, 8 1, 5<, 8 1, 1<, 8 1, 2<, 8 1, 4<, 8 1, 3<, 8 1, 1<, 8 1, 2<, 8 1, 4<, 8 1, 1<, 8 1, 3<, 8 1, 2<, 8 1, 2<<
depths
8 1, 2, 3, 4, 5<
smokePerDepth = Map @ Length @ Cases @ depthArray @@ smokeFaultsFound DD , # DD &, depths D
8 63, 54, 31, 13, 8<
Apply @ Plus, smokePerDepth D
169
N @ smokePerDepth ê totalPerDepth D
8 0.940299, 0.915254, 0.815789, 0.590909, 0.571429<
foundDepths = Map @8 1, # < &, depthArray @@ faultsFound DDD ;
faultsPerDepth = Map @ Length @ Cases @ depthArray @@ faultsFound DD , # DD &, depths D ;
gr = BarChart @ N @ faultsPerDepth ê totalPerDepth D , Frame → True, ImageSize → 8 200, Automatic ü Computation
Clear[plotHistogram]; plotHistogram[type_, Application_]:= Module[{}, depthArray = Get[("FaultDepths_Terp"<>Application<>".txt")]; depths=Union[depthArray]; totalPerDepth= Map[Length[Cases[depthArray, #]]&, depths]; matrix = Get[(type <> "_Terp"<>Application<>".txt")]; zeros = Position[Map[Apply[Plus, #]&, Transpose[matrix]], 0]; faultsFound = Delete[Range[Length[Transpose[matrix]]], zeros]; foundDepths=Map[{1, #}&, depthArray[[faultsFound]]]; faultsPerDepth=Map[Length[Cases[depthArray[[faultsFound]], #]]&, depths]; BarChart[N[faultsPerDepth/totalPerDepth], Frame → True, ImageSize → {200, Automatic}]]
plotHistogram["Comp", "Word"]
1 2 3 4 5
0
1 2 3 4 5
Graphics
Invoking the Function
Map[plotHistogram[#[[1]], #[[2]]]&, {{"Comp","Word"}, {"Smoke", "Word"}}]
1 2 3 4 5
0
1 2 3 4 5
Map[plotHistogram[#[[1]], #[[2]]]&, {{"Comp","SpreadSheet"}, {"Smoke", "SpreadSheet"}}]
1 2 3 4 5
0
1 2 3 4 5
1 2 3 4 5
0
1 2 3 4 5
8 Graphics , Graphics <