From f6d9a8c925bee3c5c3f36f4511d2cd8e9d7b5e8b Mon Sep 17 00:00:00 2001 From: Alexis Olson Date: Sun, 14 Apr 2024 11:38:30 -0500 Subject: [PATCH 1/2] Add List.Norm --- Functions/List/List.Norm.pq | 53 +++++++++++++++++++++++++++++++++++++ M.pq | 5 ++++ M_Creator.py | 1 + 3 files changed, 59 insertions(+) create mode 100644 Functions/List/List.Norm.pq diff --git a/Functions/List/List.Norm.pq b/Functions/List/List.Norm.pq new file mode 100644 index 0000000..3196cd8 --- /dev/null +++ b/Functions/List/List.Norm.pq @@ -0,0 +1,53 @@ +let + // Define metadata for the function, describing its purpose and usage. + metaDocumentation = type function ( + inputList as (type list meta [ + Documentation.FieldCaption = "List input" + ]), + optional p as (type number meta [ + Documentation.FieldCaption = "Select any p > 0 to use the Lp norm" + ]) + ) as number meta [ + Documentation.Name = "List.Norm", + Documentation.Author = "Alexis Olson", + Documentation.LongDescription = + // This is the description of the documentation, it only accepts a handful of HTML tags for formatting. + " + Returns the norm of the list. +

The default is the standard L² Euclidean norm but the optional argument allows any Lp norm.

+
  • Author: Alexis Olson
  • +
  • LinkedIn: https://www.linkedin.com/in/alexis-olson-81726818/
  • + ", + Documentation.Examples = { + [ + Description = " Default L² (Euclidean) norm. ", + Code = "List.Norm({1, 2, 3})", + Result = "3.7416573867739413 = √(1² + 2² + 3²)" + ], + [ + Description = " L¹ (Taxicab or Manhattan) norm. ", + Code = "List.Norm({1, -1, 1, 0}, 1)", + Result = "3 = |1| + |-1| + |1| + |0|" + ], + [ + Description = " L∞ (infinity or maximum) norm. ", + Code = "List.Norm({3, -4}, Number.PositiveInfinity)", + Result = "4" + ] + } + ], + myFunction = + (L as list, optional p as number) as number => + let + AbsL = List.Transform(L, Number.Abs), + PowerP = List.Transform(AbsL, each Number.Power(_, p ?? 2)), + RootP = Number.Power(List.Sum(PowerP), 1 / (p ?? 2)), + Result = + if p = Number.PositiveInfinity + then List.Max(AbsL) + else RootP + in + Result +in + // Apply the function metadata to myFunction. + Value.ReplaceType(myFunction, metaDocumentation) \ No newline at end of file diff --git a/M.pq b/M.pq index 153db11..3750964 100644 --- a/M.pq +++ b/M.pq @@ -102,12 +102,17 @@ let List.Dates = List.Dates, List.IsEmpty = List.IsEmpty, List.MatchesAny = List.MatchesAny, + List.Max = List.Max, List.RemoveItems = List.RemoveItems, + List.Sum = List.Sum, List.Transform = List.Transform, List.Type = List.Type, Logical.Type = Logical.Type, + Number.Abs = Number.Abs, Number.From = Number.From, Number.IntegerDivide = Number.IntegerDivide, + Number.Power = Number.Power, + Number.PositiveInfinity = Number.PositiveInfinity, Number.Type = Number.Type, Splitter.SplitByNothing = Splitter.SplitByNothing, Splitter.SplitTextByRepeatedLengths = Splitter.SplitTextByRepeatedLengths, diff --git a/M_Creator.py b/M_Creator.py index d0400b6..277aa2e 100644 --- a/M_Creator.py +++ b/M_Creator.py @@ -20,6 +20,7 @@ 'Formatting.IsMultiLine', 'gorilla.bi', 'List.Flatten', + 'List.Norm', 'microsoft.com', 'odata.nextLink', 'Table.ToM', From 7ad81afb1ac6eb965c8c17f0cfe9004130770d1c Mon Sep 17 00:00:00 2001 From: Alexis Olson Date: Sun, 14 Apr 2024 11:52:41 -0500 Subject: [PATCH 2/2] Add List.DotProduct --- Functions/List/List.DotProduct.pq | 34 +++++++++++++++++++++++++++++++ M.pq | 2 ++ M_Creator.py | 1 + 3 files changed, 37 insertions(+) create mode 100644 Functions/List/List.DotProduct.pq diff --git a/Functions/List/List.DotProduct.pq b/Functions/List/List.DotProduct.pq new file mode 100644 index 0000000..287738b --- /dev/null +++ b/Functions/List/List.DotProduct.pq @@ -0,0 +1,34 @@ +let + // Define metadata for the function, describing its purpose and usage. + metaDocumentation = type function ( + list1 as (type list), + list2 as (type list) + ) as number meta [ + Documentation.Name = "List.DotProduct", + Documentation.Author = "Alexis Olson", + Documentation.LongDescription = + // This is the description of the documentation, it only accepts a handful of HTML tags for formatting. + " + Returns the dot product of two lists. +

    +
  • Author: Alexis Olson
  • +
  • LinkedIn: https://www.linkedin.com/in/alexis-olson-81726818/
  • + ", + Documentation.Examples = { + [ + Description = " Compute the dot product of {1, 3, -5} and {4, -2, -1}.", + Code = "List.DotProduct({1, 3, -5}, {4, -2, -1})", + Result = "3 = (1 * 4) + (3 * -2) + (-5 * -1)" + ] + } + ], + myFunction = + (list1 as list, list2 as list) as number => + let + Zip = List.Zip({list1, list2}), + Result = List.Sum(List.Transform(Zip, List.Product)) + in + Result +in + // Apply the function metadata to myFunction. + Value.ReplaceType(myFunction, metaDocumentation) \ No newline at end of file diff --git a/M.pq b/M.pq index 3750964..a998dbc 100644 --- a/M.pq +++ b/M.pq @@ -103,10 +103,12 @@ let List.IsEmpty = List.IsEmpty, List.MatchesAny = List.MatchesAny, List.Max = List.Max, + List.Product = List.Product, List.RemoveItems = List.RemoveItems, List.Sum = List.Sum, List.Transform = List.Transform, List.Type = List.Type, + List.Zip = List.Zip, Logical.Type = Logical.Type, Number.Abs = Number.Abs, Number.From = Number.From, diff --git a/M_Creator.py b/M_Creator.py index 277aa2e..a87994e 100644 --- a/M_Creator.py +++ b/M_Creator.py @@ -21,6 +21,7 @@ 'gorilla.bi', 'List.Flatten', 'List.Norm', + 'List.DotProduct' 'microsoft.com', 'odata.nextLink', 'Table.ToM',