Skip to content

Commit

Permalink
Merge pull request #6 from AlexisOlson/Vectors
Browse files Browse the repository at this point in the history
Add List.Norm and List.DotProduct
  • Loading branch information
OscarValerock authored Apr 15, 2024
2 parents 17868cf + 7ad81af commit 47d3a57
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Functions/List/List.DotProduct.pq
Original file line number Diff line number Diff line change
@@ -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.
<p></p>
<li><b>Author: </b>Alexis Olson</li>
<li><b>LinkedIn: </b>https://www.linkedin.com/in/alexis-olson-81726818/</li>
",
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)
53 changes: 53 additions & 0 deletions Functions/List/List.Norm.pq
Original file line number Diff line number Diff line change
@@ -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.
<p>The default is the standard L² Euclidean norm but the optional argument allows any Lp norm.</p>
<li><b>Author: </b>Alexis Olson</li>
<li><b>LinkedIn: </b>https://www.linkedin.com/in/alexis-olson-81726818/</li>
",
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)
7 changes: 7 additions & 0 deletions M.pq
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,19 @@ let
List.Dates = List.Dates,
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,
Number.IntegerDivide = Number.IntegerDivide,
Number.Power = Number.Power,
Number.PositiveInfinity = Number.PositiveInfinity,
Number.Type = Number.Type,
Splitter.SplitByNothing = Splitter.SplitByNothing,
Splitter.SplitTextByRepeatedLengths = Splitter.SplitTextByRepeatedLengths,
Expand Down
2 changes: 2 additions & 0 deletions M_Creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
'Formatting.IsMultiLine',
'gorilla.bi',
'List.Flatten',
'List.Norm',
'List.DotProduct'
'microsoft.com',
'odata.nextLink',
'Table.ToM',
Expand Down

0 comments on commit 47d3a57

Please sign in to comment.