id
int32
0
165k
repo
stringlengths
7
58
path
stringlengths
12
218
func_name
stringlengths
3
140
original_string
stringlengths
73
34.1k
language
stringclasses
1 value
code
stringlengths
73
34.1k
code_tokens
list
docstring
stringlengths
3
16k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
105
339
17,400
haifengl/smile
core/src/main/java/smile/association/FPGrowth.java
FPGrowth.grow
private long grow(PrintStream out, List<ItemSet> list, TotalSupportTree ttree, FPTree.Node node, int[] itemset, int support) { int height = 0; for (FPTree.Node currentNode = node; currentNode != null; currentNode = currentNode.parent) { height ++; } int n = 0; if (height > 0) { int[] items = new int[height]; int i = 0; for (FPTree.Node currentNode = node; currentNode != null; currentNode = currentNode.parent) { items[i ++] = currentNode.id; } int[] itemIndexStack = new int[height]; int itemIndexStackPos = 0; itemset = insert(itemset, items[itemIndexStack[itemIndexStackPos]]); collect(out, list, ttree, itemset, support); n ++; while (itemIndexStack[0] < height - 1) { if (itemIndexStack[itemIndexStackPos] < height - 1) { itemIndexStackPos ++; itemIndexStack[itemIndexStackPos] = itemIndexStack[itemIndexStackPos - 1] + 1; itemset = insert(itemset, items[itemIndexStack[itemIndexStackPos]]); collect(out, list, ttree, itemset, support); n ++; } else { itemset = drop(itemset); if (itemset != null) { itemIndexStackPos --; itemIndexStack[itemIndexStackPos] = itemIndexStack[itemIndexStackPos] + 1; itemset[0] = items[itemIndexStack[itemIndexStackPos]]; collect(out, list, ttree, itemset, support); n ++; } } } } return n; }
java
private long grow(PrintStream out, List<ItemSet> list, TotalSupportTree ttree, FPTree.Node node, int[] itemset, int support) { int height = 0; for (FPTree.Node currentNode = node; currentNode != null; currentNode = currentNode.parent) { height ++; } int n = 0; if (height > 0) { int[] items = new int[height]; int i = 0; for (FPTree.Node currentNode = node; currentNode != null; currentNode = currentNode.parent) { items[i ++] = currentNode.id; } int[] itemIndexStack = new int[height]; int itemIndexStackPos = 0; itemset = insert(itemset, items[itemIndexStack[itemIndexStackPos]]); collect(out, list, ttree, itemset, support); n ++; while (itemIndexStack[0] < height - 1) { if (itemIndexStack[itemIndexStackPos] < height - 1) { itemIndexStackPos ++; itemIndexStack[itemIndexStackPos] = itemIndexStack[itemIndexStackPos - 1] + 1; itemset = insert(itemset, items[itemIndexStack[itemIndexStackPos]]); collect(out, list, ttree, itemset, support); n ++; } else { itemset = drop(itemset); if (itemset != null) { itemIndexStackPos --; itemIndexStack[itemIndexStackPos] = itemIndexStack[itemIndexStackPos] + 1; itemset[0] = items[itemIndexStack[itemIndexStackPos]]; collect(out, list, ttree, itemset, support); n ++; } } } } return n; }
[ "private", "long", "grow", "(", "PrintStream", "out", ",", "List", "<", "ItemSet", ">", "list", ",", "TotalSupportTree", "ttree", ",", "FPTree", ".", "Node", "node", ",", "int", "[", "]", "itemset", ",", "int", "support", ")", "{", "int", "height", "=", "0", ";", "for", "(", "FPTree", ".", "Node", "currentNode", "=", "node", ";", "currentNode", "!=", "null", ";", "currentNode", "=", "currentNode", ".", "parent", ")", "{", "height", "++", ";", "}", "int", "n", "=", "0", ";", "if", "(", "height", ">", "0", ")", "{", "int", "[", "]", "items", "=", "new", "int", "[", "height", "]", ";", "int", "i", "=", "0", ";", "for", "(", "FPTree", ".", "Node", "currentNode", "=", "node", ";", "currentNode", "!=", "null", ";", "currentNode", "=", "currentNode", ".", "parent", ")", "{", "items", "[", "i", "++", "]", "=", "currentNode", ".", "id", ";", "}", "int", "[", "]", "itemIndexStack", "=", "new", "int", "[", "height", "]", ";", "int", "itemIndexStackPos", "=", "0", ";", "itemset", "=", "insert", "(", "itemset", ",", "items", "[", "itemIndexStack", "[", "itemIndexStackPos", "]", "]", ")", ";", "collect", "(", "out", ",", "list", ",", "ttree", ",", "itemset", ",", "support", ")", ";", "n", "++", ";", "while", "(", "itemIndexStack", "[", "0", "]", "<", "height", "-", "1", ")", "{", "if", "(", "itemIndexStack", "[", "itemIndexStackPos", "]", "<", "height", "-", "1", ")", "{", "itemIndexStackPos", "++", ";", "itemIndexStack", "[", "itemIndexStackPos", "]", "=", "itemIndexStack", "[", "itemIndexStackPos", "-", "1", "]", "+", "1", ";", "itemset", "=", "insert", "(", "itemset", ",", "items", "[", "itemIndexStack", "[", "itemIndexStackPos", "]", "]", ")", ";", "collect", "(", "out", ",", "list", ",", "ttree", ",", "itemset", ",", "support", ")", ";", "n", "++", ";", "}", "else", "{", "itemset", "=", "drop", "(", "itemset", ")", ";", "if", "(", "itemset", "!=", "null", ")", "{", "itemIndexStackPos", "--", ";", "itemIndexStack", "[", "itemIndexStackPos", "]", "=", "itemIndexStack", "[", "itemIndexStackPos", "]", "+", "1", ";", "itemset", "[", "0", "]", "=", "items", "[", "itemIndexStack", "[", "itemIndexStackPos", "]", "]", ";", "collect", "(", "out", ",", "list", ",", "ttree", ",", "itemset", ",", "support", ")", ";", "n", "++", ";", "}", "}", "}", "}", "return", "n", ";", "}" ]
Mines all combinations along a single path tree
[ "Mines", "all", "combinations", "along", "a", "single", "path", "tree" ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/FPGrowth.java#L346-L386
17,401
haifengl/smile
core/src/main/java/smile/association/FPGrowth.java
FPGrowth.grow
private long grow(PrintStream out, List<ItemSet> list, TotalSupportTree ttree, HeaderTableItem header, int[] itemset, int[] localItemSupport, int[] prefixItemset) { long n = 1; int support = header.count; int item = header.id; itemset = insert(itemset, item); collect(out, list, ttree, itemset, support); if (header.node.next == null) { FPTree.Node node = header.node; n += grow(out, list, ttree, node.parent, itemset, support); } else { // Count singles in linked list if (getLocalItemSupport(header.node, localItemSupport)) { // Create local FP tree FPTree fptree = getLocalFPTree(header.node, localItemSupport, prefixItemset); // Mine new FP-tree n += grow(out, list, ttree, fptree, itemset, localItemSupport, prefixItemset); } } return n; }
java
private long grow(PrintStream out, List<ItemSet> list, TotalSupportTree ttree, HeaderTableItem header, int[] itemset, int[] localItemSupport, int[] prefixItemset) { long n = 1; int support = header.count; int item = header.id; itemset = insert(itemset, item); collect(out, list, ttree, itemset, support); if (header.node.next == null) { FPTree.Node node = header.node; n += grow(out, list, ttree, node.parent, itemset, support); } else { // Count singles in linked list if (getLocalItemSupport(header.node, localItemSupport)) { // Create local FP tree FPTree fptree = getLocalFPTree(header.node, localItemSupport, prefixItemset); // Mine new FP-tree n += grow(out, list, ttree, fptree, itemset, localItemSupport, prefixItemset); } } return n; }
[ "private", "long", "grow", "(", "PrintStream", "out", ",", "List", "<", "ItemSet", ">", "list", ",", "TotalSupportTree", "ttree", ",", "HeaderTableItem", "header", ",", "int", "[", "]", "itemset", ",", "int", "[", "]", "localItemSupport", ",", "int", "[", "]", "prefixItemset", ")", "{", "long", "n", "=", "1", ";", "int", "support", "=", "header", ".", "count", ";", "int", "item", "=", "header", ".", "id", ";", "itemset", "=", "insert", "(", "itemset", ",", "item", ")", ";", "collect", "(", "out", ",", "list", ",", "ttree", ",", "itemset", ",", "support", ")", ";", "if", "(", "header", ".", "node", ".", "next", "==", "null", ")", "{", "FPTree", ".", "Node", "node", "=", "header", ".", "node", ";", "n", "+=", "grow", "(", "out", ",", "list", ",", "ttree", ",", "node", ".", "parent", ",", "itemset", ",", "support", ")", ";", "}", "else", "{", "// Count singles in linked list", "if", "(", "getLocalItemSupport", "(", "header", ".", "node", ",", "localItemSupport", ")", ")", "{", "// Create local FP tree", "FPTree", "fptree", "=", "getLocalFPTree", "(", "header", ".", "node", ",", "localItemSupport", ",", "prefixItemset", ")", ";", "// Mine new FP-tree", "n", "+=", "grow", "(", "out", ",", "list", ",", "ttree", ",", "fptree", ",", "itemset", ",", "localItemSupport", ",", "prefixItemset", ")", ";", "}", "}", "return", "n", ";", "}" ]
Mines FP-tree with respect to a single element in the header table. @param header the header table item of interest. @param itemset the item set represented by the current FP-tree.
[ "Mines", "FP", "-", "tree", "with", "respect", "to", "a", "single", "element", "in", "the", "header", "table", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/FPGrowth.java#L393-L415
17,402
haifengl/smile
core/src/main/java/smile/association/FPGrowth.java
FPGrowth.getLocalItemSupport
private boolean getLocalItemSupport(FPTree.Node node, int[] localItemSupport) { boolean end = true; Arrays.fill(localItemSupport, 0); while (node != null) { int support = node.count; Node parent = node.parent; while (parent != null) { localItemSupport[parent.id] += support; parent = parent.parent; end = false; } node = node.next; } return !end; }
java
private boolean getLocalItemSupport(FPTree.Node node, int[] localItemSupport) { boolean end = true; Arrays.fill(localItemSupport, 0); while (node != null) { int support = node.count; Node parent = node.parent; while (parent != null) { localItemSupport[parent.id] += support; parent = parent.parent; end = false; } node = node.next; } return !end; }
[ "private", "boolean", "getLocalItemSupport", "(", "FPTree", ".", "Node", "node", ",", "int", "[", "]", "localItemSupport", ")", "{", "boolean", "end", "=", "true", ";", "Arrays", ".", "fill", "(", "localItemSupport", ",", "0", ")", ";", "while", "(", "node", "!=", "null", ")", "{", "int", "support", "=", "node", ".", "count", ";", "Node", "parent", "=", "node", ".", "parent", ";", "while", "(", "parent", "!=", "null", ")", "{", "localItemSupport", "[", "parent", ".", "id", "]", "+=", "support", ";", "parent", "=", "parent", ".", "parent", ";", "end", "=", "false", ";", "}", "node", "=", "node", ".", "next", ";", "}", "return", "!", "end", ";", "}" ]
Counts the supports of single items in ancestor item sets linked list. @return true if there are condition patterns given this node
[ "Counts", "the", "supports", "of", "single", "items", "in", "ancestor", "item", "sets", "linked", "list", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/FPGrowth.java#L421-L437
17,403
haifengl/smile
core/src/main/java/smile/association/FPGrowth.java
FPGrowth.getLocalFPTree
private FPTree getLocalFPTree(FPTree.Node node, int[] localItemSupport, int[] prefixItemset) { FPTree tree = new FPTree(localItemSupport, minSupport); while (node != null) { Node parent = node.parent; int i = prefixItemset.length; while (parent != null) { if (localItemSupport[parent.id] >= minSupport) { prefixItemset[--i] = parent.id; } parent = parent.parent; } if (i < prefixItemset.length) { tree.add(i, prefixItemset.length, prefixItemset, node.count); } node = node.next; } return tree; }
java
private FPTree getLocalFPTree(FPTree.Node node, int[] localItemSupport, int[] prefixItemset) { FPTree tree = new FPTree(localItemSupport, minSupport); while (node != null) { Node parent = node.parent; int i = prefixItemset.length; while (parent != null) { if (localItemSupport[parent.id] >= minSupport) { prefixItemset[--i] = parent.id; } parent = parent.parent; } if (i < prefixItemset.length) { tree.add(i, prefixItemset.length, prefixItemset, node.count); } node = node.next; } return tree; }
[ "private", "FPTree", "getLocalFPTree", "(", "FPTree", ".", "Node", "node", ",", "int", "[", "]", "localItemSupport", ",", "int", "[", "]", "prefixItemset", ")", "{", "FPTree", "tree", "=", "new", "FPTree", "(", "localItemSupport", ",", "minSupport", ")", ";", "while", "(", "node", "!=", "null", ")", "{", "Node", "parent", "=", "node", ".", "parent", ";", "int", "i", "=", "prefixItemset", ".", "length", ";", "while", "(", "parent", "!=", "null", ")", "{", "if", "(", "localItemSupport", "[", "parent", ".", "id", "]", ">=", "minSupport", ")", "{", "prefixItemset", "[", "--", "i", "]", "=", "parent", ".", "id", ";", "}", "parent", "=", "parent", ".", "parent", ";", "}", "if", "(", "i", "<", "prefixItemset", ".", "length", ")", "{", "tree", ".", "add", "(", "i", ",", "prefixItemset", ".", "length", ",", "prefixItemset", ",", "node", ".", "count", ")", ";", "}", "node", "=", "node", ".", "next", ";", "}", "return", "tree", ";", "}" ]
Generates a local FP tree @param node the conditional patterns given this node to construct the local FP-tree. @rerurn the local FP-tree.
[ "Generates", "a", "local", "FP", "tree" ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/FPGrowth.java#L444-L464
17,404
haifengl/smile
core/src/main/java/smile/association/FPGrowth.java
FPGrowth.insert
static int[] insert(int[] itemset, int item) { if (itemset == null) { int[] newItemset = {item}; return newItemset; } else { int n = itemset.length + 1; int[] newItemset = new int[n]; newItemset[0] = item; System.arraycopy(itemset, 0, newItemset, 1, n - 1); return newItemset; } }
java
static int[] insert(int[] itemset, int item) { if (itemset == null) { int[] newItemset = {item}; return newItemset; } else { int n = itemset.length + 1; int[] newItemset = new int[n]; newItemset[0] = item; System.arraycopy(itemset, 0, newItemset, 1, n - 1); return newItemset; } }
[ "static", "int", "[", "]", "insert", "(", "int", "[", "]", "itemset", ",", "int", "item", ")", "{", "if", "(", "itemset", "==", "null", ")", "{", "int", "[", "]", "newItemset", "=", "{", "item", "}", ";", "return", "newItemset", ";", "}", "else", "{", "int", "n", "=", "itemset", ".", "length", "+", "1", ";", "int", "[", "]", "newItemset", "=", "new", "int", "[", "n", "]", ";", "newItemset", "[", "0", "]", "=", "item", ";", "System", ".", "arraycopy", "(", "itemset", ",", "0", ",", "newItemset", ",", "1", ",", "n", "-", "1", ")", ";", "return", "newItemset", ";", "}", "}" ]
Insert a item to the front of an item set. @param itemset the original item set. @param item the new item to be inserted. @return the combined item set
[ "Insert", "a", "item", "to", "the", "front", "of", "an", "item", "set", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/FPGrowth.java#L472-L486
17,405
haifengl/smile
core/src/main/java/smile/association/FPGrowth.java
FPGrowth.drop
static int[] drop(int[] itemset) { if (itemset.length >= 1) { int n = itemset.length - 1; int[] newItemset = new int[n]; System.arraycopy(itemset, 1, newItemset, 0, n); return newItemset; } else { return null; } }
java
static int[] drop(int[] itemset) { if (itemset.length >= 1) { int n = itemset.length - 1; int[] newItemset = new int[n]; System.arraycopy(itemset, 1, newItemset, 0, n); return newItemset; } else { return null; } }
[ "static", "int", "[", "]", "drop", "(", "int", "[", "]", "itemset", ")", "{", "if", "(", "itemset", ".", "length", ">=", "1", ")", "{", "int", "n", "=", "itemset", ".", "length", "-", "1", ";", "int", "[", "]", "newItemset", "=", "new", "int", "[", "n", "]", ";", "System", ".", "arraycopy", "(", "itemset", ",", "1", ",", "newItemset", ",", "0", ",", "n", ")", ";", "return", "newItemset", ";", "}", "else", "{", "return", "null", ";", "}", "}" ]
Drops an item form the front of an item set. @param itemset the original item set. @return the reduced item set or null if the original is empty
[ "Drops", "an", "item", "form", "the", "front", "of", "an", "item", "set", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/FPGrowth.java#L493-L504
17,406
haifengl/smile
plot/src/main/java/smile/plot/Axis.java
Axis.setSlice
private void setSlice() { // slicing initialisation if (labels == null) { double min = base.getPrecisionUnit()[index] * Math.ceil(base.getLowerBounds()[index] / base.getPrecisionUnit()[index]); double max = base.getPrecisionUnit()[index] * Math.floor(base.getUpperBounds()[index] / base.getPrecisionUnit()[index]); linearSlices = (int) Math.ceil(Math.round((max - min) / base.getPrecisionUnit()[index], 1)); if (linearSlices <= 0) { linearSlices = 1; } if (linearSlices < 3) { linearSlices *= 2; } linesSlicing = new double[linearSlices + 3]; labelsSlicing = new double[linearSlices + 3]; double pitch = (max - min) / linearSlices; for (int i = 1; i <= linearSlices + 1; i++) { // lines and labels slicing are the same linesSlicing[i] = min + (i - 1) * pitch; labelsSlicing[i] = min + (i - 1) * pitch; } linesSlicing[0] = base.getLowerBounds()[index]; labelsSlicing[0] = base.getLowerBounds()[index]; linesSlicing[linearSlices + 2] = base.getUpperBounds()[index]; labelsSlicing[linearSlices + 2] = base.getUpperBounds()[index]; } else { linesSlicing = new double[labels.size() + 2]; labelsSlicing = new double[labels.size()]; gridLabelStrings = new String[labels.size()]; linesSlicing[0] = base.getLowerBounds()[index]; int i = 1; for (String string : labels.keySet()) { linesSlicing[i] = labels.get(string); labelsSlicing[i - 1] = labels.get(string); gridLabelStrings[i - 1] = string; i++; } linesSlicing[i] = base.getUpperBounds()[index]; Arrays.sort(linesSlicing); QuickSort.sort(labelsSlicing, gridLabelStrings); } }
java
private void setSlice() { // slicing initialisation if (labels == null) { double min = base.getPrecisionUnit()[index] * Math.ceil(base.getLowerBounds()[index] / base.getPrecisionUnit()[index]); double max = base.getPrecisionUnit()[index] * Math.floor(base.getUpperBounds()[index] / base.getPrecisionUnit()[index]); linearSlices = (int) Math.ceil(Math.round((max - min) / base.getPrecisionUnit()[index], 1)); if (linearSlices <= 0) { linearSlices = 1; } if (linearSlices < 3) { linearSlices *= 2; } linesSlicing = new double[linearSlices + 3]; labelsSlicing = new double[linearSlices + 3]; double pitch = (max - min) / linearSlices; for (int i = 1; i <= linearSlices + 1; i++) { // lines and labels slicing are the same linesSlicing[i] = min + (i - 1) * pitch; labelsSlicing[i] = min + (i - 1) * pitch; } linesSlicing[0] = base.getLowerBounds()[index]; labelsSlicing[0] = base.getLowerBounds()[index]; linesSlicing[linearSlices + 2] = base.getUpperBounds()[index]; labelsSlicing[linearSlices + 2] = base.getUpperBounds()[index]; } else { linesSlicing = new double[labels.size() + 2]; labelsSlicing = new double[labels.size()]; gridLabelStrings = new String[labels.size()]; linesSlicing[0] = base.getLowerBounds()[index]; int i = 1; for (String string : labels.keySet()) { linesSlicing[i] = labels.get(string); labelsSlicing[i - 1] = labels.get(string); gridLabelStrings[i - 1] = string; i++; } linesSlicing[i] = base.getUpperBounds()[index]; Arrays.sort(linesSlicing); QuickSort.sort(labelsSlicing, gridLabelStrings); } }
[ "private", "void", "setSlice", "(", ")", "{", "// slicing initialisation", "if", "(", "labels", "==", "null", ")", "{", "double", "min", "=", "base", ".", "getPrecisionUnit", "(", ")", "[", "index", "]", "*", "Math", ".", "ceil", "(", "base", ".", "getLowerBounds", "(", ")", "[", "index", "]", "/", "base", ".", "getPrecisionUnit", "(", ")", "[", "index", "]", ")", ";", "double", "max", "=", "base", ".", "getPrecisionUnit", "(", ")", "[", "index", "]", "*", "Math", ".", "floor", "(", "base", ".", "getUpperBounds", "(", ")", "[", "index", "]", "/", "base", ".", "getPrecisionUnit", "(", ")", "[", "index", "]", ")", ";", "linearSlices", "=", "(", "int", ")", "Math", ".", "ceil", "(", "Math", ".", "round", "(", "(", "max", "-", "min", ")", "/", "base", ".", "getPrecisionUnit", "(", ")", "[", "index", "]", ",", "1", ")", ")", ";", "if", "(", "linearSlices", "<=", "0", ")", "{", "linearSlices", "=", "1", ";", "}", "if", "(", "linearSlices", "<", "3", ")", "{", "linearSlices", "*=", "2", ";", "}", "linesSlicing", "=", "new", "double", "[", "linearSlices", "+", "3", "]", ";", "labelsSlicing", "=", "new", "double", "[", "linearSlices", "+", "3", "]", ";", "double", "pitch", "=", "(", "max", "-", "min", ")", "/", "linearSlices", ";", "for", "(", "int", "i", "=", "1", ";", "i", "<=", "linearSlices", "+", "1", ";", "i", "++", ")", "{", "// lines and labels slicing are the same", "linesSlicing", "[", "i", "]", "=", "min", "+", "(", "i", "-", "1", ")", "*", "pitch", ";", "labelsSlicing", "[", "i", "]", "=", "min", "+", "(", "i", "-", "1", ")", "*", "pitch", ";", "}", "linesSlicing", "[", "0", "]", "=", "base", ".", "getLowerBounds", "(", ")", "[", "index", "]", ";", "labelsSlicing", "[", "0", "]", "=", "base", ".", "getLowerBounds", "(", ")", "[", "index", "]", ";", "linesSlicing", "[", "linearSlices", "+", "2", "]", "=", "base", ".", "getUpperBounds", "(", ")", "[", "index", "]", ";", "labelsSlicing", "[", "linearSlices", "+", "2", "]", "=", "base", ".", "getUpperBounds", "(", ")", "[", "index", "]", ";", "}", "else", "{", "linesSlicing", "=", "new", "double", "[", "labels", ".", "size", "(", ")", "+", "2", "]", ";", "labelsSlicing", "=", "new", "double", "[", "labels", ".", "size", "(", ")", "]", ";", "gridLabelStrings", "=", "new", "String", "[", "labels", ".", "size", "(", ")", "]", ";", "linesSlicing", "[", "0", "]", "=", "base", ".", "getLowerBounds", "(", ")", "[", "index", "]", ";", "int", "i", "=", "1", ";", "for", "(", "String", "string", ":", "labels", ".", "keySet", "(", ")", ")", "{", "linesSlicing", "[", "i", "]", "=", "labels", ".", "get", "(", "string", ")", ";", "labelsSlicing", "[", "i", "-", "1", "]", "=", "labels", ".", "get", "(", "string", ")", ";", "gridLabelStrings", "[", "i", "-", "1", "]", "=", "string", ";", "i", "++", ";", "}", "linesSlicing", "[", "i", "]", "=", "base", ".", "getUpperBounds", "(", ")", "[", "index", "]", ";", "Arrays", ".", "sort", "(", "linesSlicing", ")", ";", "QuickSort", ".", "sort", "(", "labelsSlicing", ",", "gridLabelStrings", ")", ";", "}", "}" ]
Set the slices of axis.
[ "Set", "the", "slices", "of", "axis", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Axis.java#L139-L187
17,407
haifengl/smile
plot/src/main/java/smile/plot/Axis.java
Axis.initGridLines
private void initGridLines() { gridLines = new Line[base.getDimension() - 1][linesSlicing.length]; int i2 = 0; for (int i = 0; i < base.getDimension() - 1; i++) { if (i2 == index) { i2++; } for (int j = 0; j < gridLines[i].length; j++) { double[] originBase = new double[base.getDimension()]; double[] endBase = new double[base.getDimension()]; System.arraycopy(origin, 0, originBase, 0, base.getDimension()); System.arraycopy(origin, 0, endBase, 0, base.getDimension()); endBase[i2] = base.getCoordinateSpace()[i2 + 1][i2]; originBase[index] = linesSlicing[j]; endBase[index] = linesSlicing[j]; if (j == 0 || j == gridLines[i].length - 1) { gridLines[i][j] = new Line(originBase, endBase); } else { gridLines[i][j] = new Line(Line.Style.DOT, Color.lightGray, originBase, endBase); } } i2++; } }
java
private void initGridLines() { gridLines = new Line[base.getDimension() - 1][linesSlicing.length]; int i2 = 0; for (int i = 0; i < base.getDimension() - 1; i++) { if (i2 == index) { i2++; } for (int j = 0; j < gridLines[i].length; j++) { double[] originBase = new double[base.getDimension()]; double[] endBase = new double[base.getDimension()]; System.arraycopy(origin, 0, originBase, 0, base.getDimension()); System.arraycopy(origin, 0, endBase, 0, base.getDimension()); endBase[i2] = base.getCoordinateSpace()[i2 + 1][i2]; originBase[index] = linesSlicing[j]; endBase[index] = linesSlicing[j]; if (j == 0 || j == gridLines[i].length - 1) { gridLines[i][j] = new Line(originBase, endBase); } else { gridLines[i][j] = new Line(Line.Style.DOT, Color.lightGray, originBase, endBase); } } i2++; } }
[ "private", "void", "initGridLines", "(", ")", "{", "gridLines", "=", "new", "Line", "[", "base", ".", "getDimension", "(", ")", "-", "1", "]", "[", "linesSlicing", ".", "length", "]", ";", "int", "i2", "=", "0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "base", ".", "getDimension", "(", ")", "-", "1", ";", "i", "++", ")", "{", "if", "(", "i2", "==", "index", ")", "{", "i2", "++", ";", "}", "for", "(", "int", "j", "=", "0", ";", "j", "<", "gridLines", "[", "i", "]", ".", "length", ";", "j", "++", ")", "{", "double", "[", "]", "originBase", "=", "new", "double", "[", "base", ".", "getDimension", "(", ")", "]", ";", "double", "[", "]", "endBase", "=", "new", "double", "[", "base", ".", "getDimension", "(", ")", "]", ";", "System", ".", "arraycopy", "(", "origin", ",", "0", ",", "originBase", ",", "0", ",", "base", ".", "getDimension", "(", ")", ")", ";", "System", ".", "arraycopy", "(", "origin", ",", "0", ",", "endBase", ",", "0", ",", "base", ".", "getDimension", "(", ")", ")", ";", "endBase", "[", "i2", "]", "=", "base", ".", "getCoordinateSpace", "(", ")", "[", "i2", "+", "1", "]", "[", "i2", "]", ";", "originBase", "[", "index", "]", "=", "linesSlicing", "[", "j", "]", ";", "endBase", "[", "index", "]", "=", "linesSlicing", "[", "j", "]", ";", "if", "(", "j", "==", "0", "||", "j", "==", "gridLines", "[", "i", "]", ".", "length", "-", "1", ")", "{", "gridLines", "[", "i", "]", "[", "j", "]", "=", "new", "Line", "(", "originBase", ",", "endBase", ")", ";", "}", "else", "{", "gridLines", "[", "i", "]", "[", "j", "]", "=", "new", "Line", "(", "Line", ".", "Style", ".", "DOT", ",", "Color", ".", "lightGray", ",", "originBase", ",", "endBase", ")", ";", "}", "}", "i2", "++", ";", "}", "}" ]
Initialize grid lines.
[ "Initialize", "grid", "lines", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Axis.java#L267-L296
17,408
haifengl/smile
plot/src/main/java/smile/plot/Axis.java
Axis.removeLabel
public Axis removeLabel(String label) { if (labels == null) { throw new IllegalStateException(); } labels.remove(label); setSlice(); initGridLabels(); return this; }
java
public Axis removeLabel(String label) { if (labels == null) { throw new IllegalStateException(); } labels.remove(label); setSlice(); initGridLabels(); return this; }
[ "public", "Axis", "removeLabel", "(", "String", "label", ")", "{", "if", "(", "labels", "==", "null", ")", "{", "throw", "new", "IllegalStateException", "(", ")", ";", "}", "labels", ".", "remove", "(", "label", ")", ";", "setSlice", "(", ")", ";", "initGridLabels", "(", ")", ";", "return", "this", ";", "}" ]
Remove a label from the axis.
[ "Remove", "a", "label", "from", "the", "axis", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Axis.java#L359-L368
17,409
haifengl/smile
plot/src/main/java/smile/plot/Axis.java
Axis.setAxisLabel
public Axis setAxisLabel(String label) { if (label == null) { if (index == 0) { label = "X"; } else if (index == 1) { label = "Y"; } else if (index == 2) { label = "Z"; } } if (label != null) { double[] position = new double[base.getDimension()]; if (base.getDimension() == 2) { position[index] = 0.5; if (index == 0) { position[1] = -0.1; axisLabel = new BaseLabel(label, 0.5, 1.0, position); } else { position[0] = -0.15; axisLabel = new BaseLabel(label, 0.5, 0.5, -Math.PI / 2, position); } } else { if (index == 0) { position[2] = 1.0; position[index] = 0.5; axisLabel = new BaseLabel(label, 0.5, -2.0, position); } else if (index == 1) { position[0] = 1.0; position[index] = 0.5; axisLabel = new BaseLabel(label, 0.5, 3.0, position); } else if (index == 2) { position[1] = 1.0; position[index] = 1.0; axisLabel = new BaseLabel(label, -0.5, -1.0, position); } } axisLabel.setFont(axisLabelFont); } return this; }
java
public Axis setAxisLabel(String label) { if (label == null) { if (index == 0) { label = "X"; } else if (index == 1) { label = "Y"; } else if (index == 2) { label = "Z"; } } if (label != null) { double[] position = new double[base.getDimension()]; if (base.getDimension() == 2) { position[index] = 0.5; if (index == 0) { position[1] = -0.1; axisLabel = new BaseLabel(label, 0.5, 1.0, position); } else { position[0] = -0.15; axisLabel = new BaseLabel(label, 0.5, 0.5, -Math.PI / 2, position); } } else { if (index == 0) { position[2] = 1.0; position[index] = 0.5; axisLabel = new BaseLabel(label, 0.5, -2.0, position); } else if (index == 1) { position[0] = 1.0; position[index] = 0.5; axisLabel = new BaseLabel(label, 0.5, 3.0, position); } else if (index == 2) { position[1] = 1.0; position[index] = 1.0; axisLabel = new BaseLabel(label, -0.5, -1.0, position); } } axisLabel.setFont(axisLabelFont); } return this; }
[ "public", "Axis", "setAxisLabel", "(", "String", "label", ")", "{", "if", "(", "label", "==", "null", ")", "{", "if", "(", "index", "==", "0", ")", "{", "label", "=", "\"X\"", ";", "}", "else", "if", "(", "index", "==", "1", ")", "{", "label", "=", "\"Y\"", ";", "}", "else", "if", "(", "index", "==", "2", ")", "{", "label", "=", "\"Z\"", ";", "}", "}", "if", "(", "label", "!=", "null", ")", "{", "double", "[", "]", "position", "=", "new", "double", "[", "base", ".", "getDimension", "(", ")", "]", ";", "if", "(", "base", ".", "getDimension", "(", ")", "==", "2", ")", "{", "position", "[", "index", "]", "=", "0.5", ";", "if", "(", "index", "==", "0", ")", "{", "position", "[", "1", "]", "=", "-", "0.1", ";", "axisLabel", "=", "new", "BaseLabel", "(", "label", ",", "0.5", ",", "1.0", ",", "position", ")", ";", "}", "else", "{", "position", "[", "0", "]", "=", "-", "0.15", ";", "axisLabel", "=", "new", "BaseLabel", "(", "label", ",", "0.5", ",", "0.5", ",", "-", "Math", ".", "PI", "/", "2", ",", "position", ")", ";", "}", "}", "else", "{", "if", "(", "index", "==", "0", ")", "{", "position", "[", "2", "]", "=", "1.0", ";", "position", "[", "index", "]", "=", "0.5", ";", "axisLabel", "=", "new", "BaseLabel", "(", "label", ",", "0.5", ",", "-", "2.0", ",", "position", ")", ";", "}", "else", "if", "(", "index", "==", "1", ")", "{", "position", "[", "0", "]", "=", "1.0", ";", "position", "[", "index", "]", "=", "0.5", ";", "axisLabel", "=", "new", "BaseLabel", "(", "label", ",", "0.5", ",", "3.0", ",", "position", ")", ";", "}", "else", "if", "(", "index", "==", "2", ")", "{", "position", "[", "1", "]", "=", "1.0", ";", "position", "[", "index", "]", "=", "1.0", ";", "axisLabel", "=", "new", "BaseLabel", "(", "label", ",", "-", "0.5", ",", "-", "1.0", ",", "position", ")", ";", "}", "}", "axisLabel", ".", "setFont", "(", "axisLabelFont", ")", ";", "}", "return", "this", ";", "}" ]
Sets the label of this axis.
[ "Sets", "the", "label", "of", "this", "axis", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Axis.java#L425-L467
17,410
haifengl/smile
plot/src/main/java/smile/plot/Axis.java
Axis.paint
public void paint(Graphics g) { if (gridLines != null) { if (gridVisible) { for (int i = 0; i < gridLines.length; i++) { for (int j = 1; j < gridLines[i].length - 1; j++) { gridLines[i][j].paint(g); } } } if (frameVisible) { for (int i = 0; i < gridLines.length; i++) { gridLines[i][0].paint(g); gridLines[i][gridLines[i].length - 1].paint(g); } } } if (labelVisible) { if (gridLabels != null) { int[] xy = g.projection.screenProjection(gridLabels[1].getCoordinate()); int prevx = xy[0]; int prevy = xy[1]; for (int i = 0; i < gridLabels.length; i++) { if (!gridLabels[i].text.isEmpty()) { double[] coord = gridLabels[i].getCoordinate(); xy = g.projection.screenProjection(coord); int x = xy[0]; int y = xy[1]; if (base.getDimension() == 2 && index == 0 && rotation != 0.0) { if ((prevx == x && prevy == y) || Math.abs(x - prevx) > gridLabels[i].font.getSize()) { gridLabels[i].paint(g); prevx = x; prevy = y; } } else if (base.getDimension() == 2 && index == 1) { if ((prevx == x && prevy == y && i == 0) || Math.abs(prevy - y) > gridLabels[i].font.getSize()) { gridLabels[i].paint(g); prevx = x; prevy = y; } } else { if ((prevx == x && prevy == y) || Math.abs(x - prevx) > g.g2d.getFontMetrics(gridLabels[i].font).stringWidth(gridLabels[i].text) || Math.abs(prevy - y) > gridLabels[i].font.getSize()) { gridLabels[i].paint(g); prevx = x; prevy = y; } } } } } if (axisLabel != null) { axisLabel.paint(g); } } }
java
public void paint(Graphics g) { if (gridLines != null) { if (gridVisible) { for (int i = 0; i < gridLines.length; i++) { for (int j = 1; j < gridLines[i].length - 1; j++) { gridLines[i][j].paint(g); } } } if (frameVisible) { for (int i = 0; i < gridLines.length; i++) { gridLines[i][0].paint(g); gridLines[i][gridLines[i].length - 1].paint(g); } } } if (labelVisible) { if (gridLabels != null) { int[] xy = g.projection.screenProjection(gridLabels[1].getCoordinate()); int prevx = xy[0]; int prevy = xy[1]; for (int i = 0; i < gridLabels.length; i++) { if (!gridLabels[i].text.isEmpty()) { double[] coord = gridLabels[i].getCoordinate(); xy = g.projection.screenProjection(coord); int x = xy[0]; int y = xy[1]; if (base.getDimension() == 2 && index == 0 && rotation != 0.0) { if ((prevx == x && prevy == y) || Math.abs(x - prevx) > gridLabels[i].font.getSize()) { gridLabels[i].paint(g); prevx = x; prevy = y; } } else if (base.getDimension() == 2 && index == 1) { if ((prevx == x && prevy == y && i == 0) || Math.abs(prevy - y) > gridLabels[i].font.getSize()) { gridLabels[i].paint(g); prevx = x; prevy = y; } } else { if ((prevx == x && prevy == y) || Math.abs(x - prevx) > g.g2d.getFontMetrics(gridLabels[i].font).stringWidth(gridLabels[i].text) || Math.abs(prevy - y) > gridLabels[i].font.getSize()) { gridLabels[i].paint(g); prevx = x; prevy = y; } } } } } if (axisLabel != null) { axisLabel.paint(g); } } }
[ "public", "void", "paint", "(", "Graphics", "g", ")", "{", "if", "(", "gridLines", "!=", "null", ")", "{", "if", "(", "gridVisible", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "gridLines", ".", "length", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "1", ";", "j", "<", "gridLines", "[", "i", "]", ".", "length", "-", "1", ";", "j", "++", ")", "{", "gridLines", "[", "i", "]", "[", "j", "]", ".", "paint", "(", "g", ")", ";", "}", "}", "}", "if", "(", "frameVisible", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "gridLines", ".", "length", ";", "i", "++", ")", "{", "gridLines", "[", "i", "]", "[", "0", "]", ".", "paint", "(", "g", ")", ";", "gridLines", "[", "i", "]", "[", "gridLines", "[", "i", "]", ".", "length", "-", "1", "]", ".", "paint", "(", "g", ")", ";", "}", "}", "}", "if", "(", "labelVisible", ")", "{", "if", "(", "gridLabels", "!=", "null", ")", "{", "int", "[", "]", "xy", "=", "g", ".", "projection", ".", "screenProjection", "(", "gridLabels", "[", "1", "]", ".", "getCoordinate", "(", ")", ")", ";", "int", "prevx", "=", "xy", "[", "0", "]", ";", "int", "prevy", "=", "xy", "[", "1", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "gridLabels", ".", "length", ";", "i", "++", ")", "{", "if", "(", "!", "gridLabels", "[", "i", "]", ".", "text", ".", "isEmpty", "(", ")", ")", "{", "double", "[", "]", "coord", "=", "gridLabels", "[", "i", "]", ".", "getCoordinate", "(", ")", ";", "xy", "=", "g", ".", "projection", ".", "screenProjection", "(", "coord", ")", ";", "int", "x", "=", "xy", "[", "0", "]", ";", "int", "y", "=", "xy", "[", "1", "]", ";", "if", "(", "base", ".", "getDimension", "(", ")", "==", "2", "&&", "index", "==", "0", "&&", "rotation", "!=", "0.0", ")", "{", "if", "(", "(", "prevx", "==", "x", "&&", "prevy", "==", "y", ")", "||", "Math", ".", "abs", "(", "x", "-", "prevx", ")", ">", "gridLabels", "[", "i", "]", ".", "font", ".", "getSize", "(", ")", ")", "{", "gridLabels", "[", "i", "]", ".", "paint", "(", "g", ")", ";", "prevx", "=", "x", ";", "prevy", "=", "y", ";", "}", "}", "else", "if", "(", "base", ".", "getDimension", "(", ")", "==", "2", "&&", "index", "==", "1", ")", "{", "if", "(", "(", "prevx", "==", "x", "&&", "prevy", "==", "y", "&&", "i", "==", "0", ")", "||", "Math", ".", "abs", "(", "prevy", "-", "y", ")", ">", "gridLabels", "[", "i", "]", ".", "font", ".", "getSize", "(", ")", ")", "{", "gridLabels", "[", "i", "]", ".", "paint", "(", "g", ")", ";", "prevx", "=", "x", ";", "prevy", "=", "y", ";", "}", "}", "else", "{", "if", "(", "(", "prevx", "==", "x", "&&", "prevy", "==", "y", ")", "||", "Math", ".", "abs", "(", "x", "-", "prevx", ")", ">", "g", ".", "g2d", ".", "getFontMetrics", "(", "gridLabels", "[", "i", "]", ".", "font", ")", ".", "stringWidth", "(", "gridLabels", "[", "i", "]", ".", "text", ")", "||", "Math", ".", "abs", "(", "prevy", "-", "y", ")", ">", "gridLabels", "[", "i", "]", ".", "font", ".", "getSize", "(", ")", ")", "{", "gridLabels", "[", "i", "]", ".", "paint", "(", "g", ")", ";", "prevx", "=", "x", ";", "prevy", "=", "y", ";", "}", "}", "}", "}", "}", "if", "(", "axisLabel", "!=", "null", ")", "{", "axisLabel", ".", "paint", "(", "g", ")", ";", "}", "}", "}" ]
Draw the axis.
[ "Draw", "the", "axis", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Axis.java#L483-L544
17,411
haifengl/smile
core/src/main/java/smile/gap/BitString.java
BitString.singlePointCrossover
private void singlePointCrossover(BitString father, BitString mother, BitString[] offsprings) { int point = 0; // crossover point while (point == 0) { point = Math.randomInt(length); } int[] son = new int[length]; System.arraycopy(father.bits, 0, son, 0, point); System.arraycopy(mother.bits, point, son, point, length - point); int[] daughter = new int[length]; System.arraycopy(mother.bits, 0, daughter, 0, point); System.arraycopy(father.bits, point, daughter, point, length - point); offsprings[0] = new BitString(son, measure, crossover, crossoverRate, mutationRate); offsprings[1] = new BitString(daughter, measure, crossover, crossoverRate, mutationRate); }
java
private void singlePointCrossover(BitString father, BitString mother, BitString[] offsprings) { int point = 0; // crossover point while (point == 0) { point = Math.randomInt(length); } int[] son = new int[length]; System.arraycopy(father.bits, 0, son, 0, point); System.arraycopy(mother.bits, point, son, point, length - point); int[] daughter = new int[length]; System.arraycopy(mother.bits, 0, daughter, 0, point); System.arraycopy(father.bits, point, daughter, point, length - point); offsprings[0] = new BitString(son, measure, crossover, crossoverRate, mutationRate); offsprings[1] = new BitString(daughter, measure, crossover, crossoverRate, mutationRate); }
[ "private", "void", "singlePointCrossover", "(", "BitString", "father", ",", "BitString", "mother", ",", "BitString", "[", "]", "offsprings", ")", "{", "int", "point", "=", "0", ";", "// crossover point", "while", "(", "point", "==", "0", ")", "{", "point", "=", "Math", ".", "randomInt", "(", "length", ")", ";", "}", "int", "[", "]", "son", "=", "new", "int", "[", "length", "]", ";", "System", ".", "arraycopy", "(", "father", ".", "bits", ",", "0", ",", "son", ",", "0", ",", "point", ")", ";", "System", ".", "arraycopy", "(", "mother", ".", "bits", ",", "point", ",", "son", ",", "point", ",", "length", "-", "point", ")", ";", "int", "[", "]", "daughter", "=", "new", "int", "[", "length", "]", ";", "System", ".", "arraycopy", "(", "mother", ".", "bits", ",", "0", ",", "daughter", ",", "0", ",", "point", ")", ";", "System", ".", "arraycopy", "(", "father", ".", "bits", ",", "point", ",", "daughter", ",", "point", ",", "length", "-", "point", ")", ";", "offsprings", "[", "0", "]", "=", "new", "BitString", "(", "son", ",", "measure", ",", "crossover", ",", "crossoverRate", ",", "mutationRate", ")", ";", "offsprings", "[", "1", "]", "=", "new", "BitString", "(", "daughter", ",", "measure", ",", "crossover", ",", "crossoverRate", ",", "mutationRate", ")", ";", "}" ]
Single point crossover.
[ "Single", "point", "crossover", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/gap/BitString.java#L241-L257
17,412
haifengl/smile
core/src/main/java/smile/gap/BitString.java
BitString.twoPointCrossover
private void twoPointCrossover(BitString father, BitString mother, BitString[] offsprings) { int point1 = 0; // first crossover point while (point1 == 0 || point1 == length - 1) { point1 = Math.randomInt(length); } int point2 = 0; // second crossover point while (point2 == point1 || point2 == 0 || point2 == length - 1) { point2 = Math.randomInt(length); } if (point2 < point1) { int p = point1; point1 = point2; point2 = p; } int[] son = new int[length]; System.arraycopy(father.bits, 0, son, 0, point1); System.arraycopy(mother.bits, point1, son, point1, point2 - point1); System.arraycopy(father.bits, point2, son, point2, length - point2); int[] daughter = new int[length]; System.arraycopy(mother.bits, 0, daughter, 0, point1); System.arraycopy(father.bits, point1, daughter, point1, point2 - point1); System.arraycopy(mother.bits, point2, daughter, point2, length - point2); offsprings[0] = new BitString(son, measure, crossover, crossoverRate, mutationRate); offsprings[1] = new BitString(daughter, measure, crossover, crossoverRate, mutationRate); }
java
private void twoPointCrossover(BitString father, BitString mother, BitString[] offsprings) { int point1 = 0; // first crossover point while (point1 == 0 || point1 == length - 1) { point1 = Math.randomInt(length); } int point2 = 0; // second crossover point while (point2 == point1 || point2 == 0 || point2 == length - 1) { point2 = Math.randomInt(length); } if (point2 < point1) { int p = point1; point1 = point2; point2 = p; } int[] son = new int[length]; System.arraycopy(father.bits, 0, son, 0, point1); System.arraycopy(mother.bits, point1, son, point1, point2 - point1); System.arraycopy(father.bits, point2, son, point2, length - point2); int[] daughter = new int[length]; System.arraycopy(mother.bits, 0, daughter, 0, point1); System.arraycopy(father.bits, point1, daughter, point1, point2 - point1); System.arraycopy(mother.bits, point2, daughter, point2, length - point2); offsprings[0] = new BitString(son, measure, crossover, crossoverRate, mutationRate); offsprings[1] = new BitString(daughter, measure, crossover, crossoverRate, mutationRate); }
[ "private", "void", "twoPointCrossover", "(", "BitString", "father", ",", "BitString", "mother", ",", "BitString", "[", "]", "offsprings", ")", "{", "int", "point1", "=", "0", ";", "// first crossover point", "while", "(", "point1", "==", "0", "||", "point1", "==", "length", "-", "1", ")", "{", "point1", "=", "Math", ".", "randomInt", "(", "length", ")", ";", "}", "int", "point2", "=", "0", ";", "// second crossover point", "while", "(", "point2", "==", "point1", "||", "point2", "==", "0", "||", "point2", "==", "length", "-", "1", ")", "{", "point2", "=", "Math", ".", "randomInt", "(", "length", ")", ";", "}", "if", "(", "point2", "<", "point1", ")", "{", "int", "p", "=", "point1", ";", "point1", "=", "point2", ";", "point2", "=", "p", ";", "}", "int", "[", "]", "son", "=", "new", "int", "[", "length", "]", ";", "System", ".", "arraycopy", "(", "father", ".", "bits", ",", "0", ",", "son", ",", "0", ",", "point1", ")", ";", "System", ".", "arraycopy", "(", "mother", ".", "bits", ",", "point1", ",", "son", ",", "point1", ",", "point2", "-", "point1", ")", ";", "System", ".", "arraycopy", "(", "father", ".", "bits", ",", "point2", ",", "son", ",", "point2", ",", "length", "-", "point2", ")", ";", "int", "[", "]", "daughter", "=", "new", "int", "[", "length", "]", ";", "System", ".", "arraycopy", "(", "mother", ".", "bits", ",", "0", ",", "daughter", ",", "0", ",", "point1", ")", ";", "System", ".", "arraycopy", "(", "father", ".", "bits", ",", "point1", ",", "daughter", ",", "point1", ",", "point2", "-", "point1", ")", ";", "System", ".", "arraycopy", "(", "mother", ".", "bits", ",", "point2", ",", "daughter", ",", "point2", ",", "length", "-", "point2", ")", ";", "offsprings", "[", "0", "]", "=", "new", "BitString", "(", "son", ",", "measure", ",", "crossover", ",", "crossoverRate", ",", "mutationRate", ")", ";", "offsprings", "[", "1", "]", "=", "new", "BitString", "(", "daughter", ",", "measure", ",", "crossover", ",", "crossoverRate", ",", "mutationRate", ")", ";", "}" ]
Two point crossover.
[ "Two", "point", "crossover", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/gap/BitString.java#L262-L291
17,413
haifengl/smile
core/src/main/java/smile/gap/BitString.java
BitString.uniformCrossover
private void uniformCrossover(BitString father, BitString mother, BitString[] offsprings) { int[] son = new int[length]; int[] daughter = new int[length]; for (int i = 0; i < length; i++) { if (Math.random() < 0.5) { son[i] = father.bits[i]; daughter[i] = mother.bits[i]; } else { son[i] = mother.bits[i]; daughter[i] = father.bits[i]; } } offsprings[0] = new BitString(son, measure, crossover, crossoverRate, mutationRate); offsprings[1] = new BitString(daughter, measure, crossover, crossoverRate, mutationRate); }
java
private void uniformCrossover(BitString father, BitString mother, BitString[] offsprings) { int[] son = new int[length]; int[] daughter = new int[length]; for (int i = 0; i < length; i++) { if (Math.random() < 0.5) { son[i] = father.bits[i]; daughter[i] = mother.bits[i]; } else { son[i] = mother.bits[i]; daughter[i] = father.bits[i]; } } offsprings[0] = new BitString(son, measure, crossover, crossoverRate, mutationRate); offsprings[1] = new BitString(daughter, measure, crossover, crossoverRate, mutationRate); }
[ "private", "void", "uniformCrossover", "(", "BitString", "father", ",", "BitString", "mother", ",", "BitString", "[", "]", "offsprings", ")", "{", "int", "[", "]", "son", "=", "new", "int", "[", "length", "]", ";", "int", "[", "]", "daughter", "=", "new", "int", "[", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "length", ";", "i", "++", ")", "{", "if", "(", "Math", ".", "random", "(", ")", "<", "0.5", ")", "{", "son", "[", "i", "]", "=", "father", ".", "bits", "[", "i", "]", ";", "daughter", "[", "i", "]", "=", "mother", ".", "bits", "[", "i", "]", ";", "}", "else", "{", "son", "[", "i", "]", "=", "mother", ".", "bits", "[", "i", "]", ";", "daughter", "[", "i", "]", "=", "father", ".", "bits", "[", "i", "]", ";", "}", "}", "offsprings", "[", "0", "]", "=", "new", "BitString", "(", "son", ",", "measure", ",", "crossover", ",", "crossoverRate", ",", "mutationRate", ")", ";", "offsprings", "[", "1", "]", "=", "new", "BitString", "(", "daughter", ",", "measure", ",", "crossover", ",", "crossoverRate", ",", "mutationRate", ")", ";", "}" ]
Uniform crossover.
[ "Uniform", "crossover", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/gap/BitString.java#L296-L312
17,414
haifengl/smile
core/src/main/java/smile/projection/PCA.java
PCA.setProjection
public PCA setProjection(int p) { if (p < 1 || p > n) { throw new IllegalArgumentException("Invalid dimension of feature space: " + p); } this.p = p; projection = Matrix.zeros(p, n); for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { projection.set(j, i, eigvectors.get(i, j)); } } pmu = new double[p]; projection.ax(mu, pmu); return this; }
java
public PCA setProjection(int p) { if (p < 1 || p > n) { throw new IllegalArgumentException("Invalid dimension of feature space: " + p); } this.p = p; projection = Matrix.zeros(p, n); for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { projection.set(j, i, eigvectors.get(i, j)); } } pmu = new double[p]; projection.ax(mu, pmu); return this; }
[ "public", "PCA", "setProjection", "(", "int", "p", ")", "{", "if", "(", "p", "<", "1", "||", "p", ">", "n", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Invalid dimension of feature space: \"", "+", "p", ")", ";", "}", "this", ".", "p", "=", "p", ";", "projection", "=", "Matrix", ".", "zeros", "(", "p", ",", "n", ")", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "0", ";", "j", "<", "p", ";", "j", "++", ")", "{", "projection", ".", "set", "(", "j", ",", "i", ",", "eigvectors", ".", "get", "(", "i", ",", "j", ")", ")", ";", "}", "}", "pmu", "=", "new", "double", "[", "p", "]", ";", "projection", ".", "ax", "(", "mu", ",", "pmu", ")", ";", "return", "this", ";", "}" ]
Set the projection matrix with given number of principal components. @param p choose top p principal components used for projection.
[ "Set", "the", "projection", "matrix", "with", "given", "number", "of", "principal", "components", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/projection/PCA.java#L251-L268
17,415
haifengl/smile
plot/src/main/java/smile/swing/table/DefaultTableHeaderCellRenderer.java
DefaultTableHeaderCellRenderer.getSortKey
@SuppressWarnings("rawtypes") protected SortKey getSortKey(JTable table, int column) { RowSorter rowSorter = table.getRowSorter(); if (rowSorter == null) { return null; } List sortedColumns = rowSorter.getSortKeys(); if (!sortedColumns.isEmpty()) { return (SortKey) sortedColumns.get(0); } return null; }
java
@SuppressWarnings("rawtypes") protected SortKey getSortKey(JTable table, int column) { RowSorter rowSorter = table.getRowSorter(); if (rowSorter == null) { return null; } List sortedColumns = rowSorter.getSortKeys(); if (!sortedColumns.isEmpty()) { return (SortKey) sortedColumns.get(0); } return null; }
[ "@", "SuppressWarnings", "(", "\"rawtypes\"", ")", "protected", "SortKey", "getSortKey", "(", "JTable", "table", ",", "int", "column", ")", "{", "RowSorter", "rowSorter", "=", "table", ".", "getRowSorter", "(", ")", ";", "if", "(", "rowSorter", "==", "null", ")", "{", "return", "null", ";", "}", "List", "sortedColumns", "=", "rowSorter", ".", "getSortKeys", "(", ")", ";", "if", "(", "!", "sortedColumns", ".", "isEmpty", "(", ")", ")", "{", "return", "(", "SortKey", ")", "sortedColumns", ".", "get", "(", "0", ")", ";", "}", "return", "null", ";", "}" ]
Returns the current sort key, or null if the column is unsorted. @param table the table @param column the column index @return the SortKey, or null if the column is unsorted
[ "Returns", "the", "current", "sort", "key", "or", "null", "if", "the", "column", "is", "unsorted", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/swing/table/DefaultTableHeaderCellRenderer.java#L122-L134
17,416
haifengl/smile
plot/src/main/java/smile/plot/PlotPanel.java
PlotPanel.organize
private void organize() { int m = (int) Math.sqrt(contentPane.getComponentCount()); if (m <= 0) m = 1; contentPane.setLayout(new GridLayout(m, 0, 0, 0)); }
java
private void organize() { int m = (int) Math.sqrt(contentPane.getComponentCount()); if (m <= 0) m = 1; contentPane.setLayout(new GridLayout(m, 0, 0, 0)); }
[ "private", "void", "organize", "(", ")", "{", "int", "m", "=", "(", "int", ")", "Math", ".", "sqrt", "(", "contentPane", ".", "getComponentCount", "(", ")", ")", ";", "if", "(", "m", "<=", "0", ")", "m", "=", "1", ";", "contentPane", ".", "setLayout", "(", "new", "GridLayout", "(", "m", ",", "0", ",", "0", ",", "0", ")", ")", ";", "}" ]
Reorganize the plots in the frame. Basically, it reset the surface layout based on the number of plots in the frame.
[ "Reorganize", "the", "plots", "in", "the", "frame", ".", "Basically", "it", "reset", "the", "surface", "layout", "based", "on", "the", "number", "of", "plots", "in", "the", "frame", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/PlotPanel.java#L114-L118
17,417
haifengl/smile
plot/src/main/java/smile/plot/PlotPanel.java
PlotPanel.initToolBar
private void initToolBar() { toolbar = new JToolBar(JToolBar.VERTICAL); toolbar.setFloatable(false); add(toolbar, BorderLayout.WEST); JButton button = makeButton("save", SAVE, "Save", "Save"); toolbar.add(button); button = makeButton("print", PRINT, "Print", "Print"); toolbar.add(button); }
java
private void initToolBar() { toolbar = new JToolBar(JToolBar.VERTICAL); toolbar.setFloatable(false); add(toolbar, BorderLayout.WEST); JButton button = makeButton("save", SAVE, "Save", "Save"); toolbar.add(button); button = makeButton("print", PRINT, "Print", "Print"); toolbar.add(button); }
[ "private", "void", "initToolBar", "(", ")", "{", "toolbar", "=", "new", "JToolBar", "(", "JToolBar", ".", "VERTICAL", ")", ";", "toolbar", ".", "setFloatable", "(", "false", ")", ";", "add", "(", "toolbar", ",", "BorderLayout", ".", "WEST", ")", ";", "JButton", "button", "=", "makeButton", "(", "\"save\"", ",", "SAVE", ",", "\"Save\"", ",", "\"Save\"", ")", ";", "toolbar", ".", "add", "(", "button", ")", ";", "button", "=", "makeButton", "(", "\"print\"", ",", "PRINT", ",", "\"Print\"", ",", "\"Print\"", ")", ";", "toolbar", ".", "add", "(", "button", ")", ";", "}" ]
Initialize toolbar.
[ "Initialize", "toolbar", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/PlotPanel.java#L139-L149
17,418
haifengl/smile
plot/src/main/java/smile/plot/PlotPanel.java
PlotPanel.makeButton
private JButton makeButton(String imageName, String actionCommand, String toolTipText, String altText) { //Look for the image. String imgLocation = "images/" + imageName + "16.png"; URL imageURL = PlotCanvas.class.getResource(imgLocation); //Create and initialize the button. JButton button = new JButton(); button.setActionCommand(actionCommand); button.setToolTipText(toolTipText); button.addActionListener(this); if (imageURL != null) { //image found button.setIcon(new ImageIcon(imageURL, altText)); } else { //no image found button.setText(altText); logger.error("Resource not found: {}", imgLocation); } return button; }
java
private JButton makeButton(String imageName, String actionCommand, String toolTipText, String altText) { //Look for the image. String imgLocation = "images/" + imageName + "16.png"; URL imageURL = PlotCanvas.class.getResource(imgLocation); //Create and initialize the button. JButton button = new JButton(); button.setActionCommand(actionCommand); button.setToolTipText(toolTipText); button.addActionListener(this); if (imageURL != null) { //image found button.setIcon(new ImageIcon(imageURL, altText)); } else { //no image found button.setText(altText); logger.error("Resource not found: {}", imgLocation); } return button; }
[ "private", "JButton", "makeButton", "(", "String", "imageName", ",", "String", "actionCommand", ",", "String", "toolTipText", ",", "String", "altText", ")", "{", "//Look for the image.", "String", "imgLocation", "=", "\"images/\"", "+", "imageName", "+", "\"16.png\"", ";", "URL", "imageURL", "=", "PlotCanvas", ".", "class", ".", "getResource", "(", "imgLocation", ")", ";", "//Create and initialize the button.", "JButton", "button", "=", "new", "JButton", "(", ")", ";", "button", ".", "setActionCommand", "(", "actionCommand", ")", ";", "button", ".", "setToolTipText", "(", "toolTipText", ")", ";", "button", ".", "addActionListener", "(", "this", ")", ";", "if", "(", "imageURL", "!=", "null", ")", "{", "//image found", "button", ".", "setIcon", "(", "new", "ImageIcon", "(", "imageURL", ",", "altText", ")", ")", ";", "}", "else", "{", "//no image found", "button", ".", "setText", "(", "altText", ")", ";", "logger", ".", "error", "(", "\"Resource not found: {}\"", ",", "imgLocation", ")", ";", "}", "return", "button", ";", "}" ]
Creates a button for toolbar.
[ "Creates", "a", "button", "for", "toolbar", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/PlotPanel.java#L154-L173
17,419
haifengl/smile
core/src/main/java/smile/util/SmileUtils.java
SmileUtils.sort
public static int[][] sort(Attribute[] attributes, double[][] x) { int n = x.length; int p = x[0].length; double[] a = new double[n]; int[][] index = new int[p][]; for (int j = 0; j < p; j++) { if (attributes[j].getType() == Attribute.Type.NUMERIC) { for (int i = 0; i < n; i++) { a[i] = x[i][j]; } index[j] = QuickSort.sort(a); } } return index; }
java
public static int[][] sort(Attribute[] attributes, double[][] x) { int n = x.length; int p = x[0].length; double[] a = new double[n]; int[][] index = new int[p][]; for (int j = 0; j < p; j++) { if (attributes[j].getType() == Attribute.Type.NUMERIC) { for (int i = 0; i < n; i++) { a[i] = x[i][j]; } index[j] = QuickSort.sort(a); } } return index; }
[ "public", "static", "int", "[", "]", "[", "]", "sort", "(", "Attribute", "[", "]", "attributes", ",", "double", "[", "]", "[", "]", "x", ")", "{", "int", "n", "=", "x", ".", "length", ";", "int", "p", "=", "x", "[", "0", "]", ".", "length", ";", "double", "[", "]", "a", "=", "new", "double", "[", "n", "]", ";", "int", "[", "]", "[", "]", "index", "=", "new", "int", "[", "p", "]", "[", "", "]", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "p", ";", "j", "++", ")", "{", "if", "(", "attributes", "[", "j", "]", ".", "getType", "(", ")", "==", "Attribute", ".", "Type", ".", "NUMERIC", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "a", "[", "i", "]", "=", "x", "[", "i", "]", "[", "j", "]", ";", "}", "index", "[", "j", "]", "=", "QuickSort", ".", "sort", "(", "a", ")", ";", "}", "}", "return", "index", ";", "}" ]
Sorts each variable and returns the index of values in ascending order. Only numeric attributes will be sorted. Note that the order of original array is NOT altered. @param x a set of variables to be sorted. Each row is an instance. Each column is a variable. @return the index of values in ascending order
[ "Sorts", "each", "variable", "and", "returns", "the", "index", "of", "values", "in", "ascending", "order", ".", "Only", "numeric", "attributes", "will", "be", "sorted", ".", "Note", "that", "the", "order", "of", "original", "array", "is", "NOT", "altered", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/util/SmileUtils.java#L49-L66
17,420
haifengl/smile
core/src/main/java/smile/imputation/SVDImputation.java
SVDImputation.impute
public void impute(double[][] data, int maxIter) throws MissingValueImputationException { if (maxIter < 1) { throw new IllegalArgumentException("Invalid maximum number of iterations: " + maxIter); } int[] count = new int[data[0].length]; for (int i = 0; i < data.length; i++) { int n = 0; for (int j = 0; j < data[i].length; j++) { if (Double.isNaN(data[i][j])) { n++; count[j]++; } } if (n == data[i].length) { throw new MissingValueImputationException("The whole row " + i + " is missing"); } } for (int i = 0; i < data[0].length; i++) { if (count[i] == data.length) { throw new MissingValueImputationException("The whole column " + i + " is missing"); } } double[][] full = new double[data.length][]; for (int i = 0; i < full.length; i++) { full[i] = data[i].clone(); } KMeansImputation.columnAverageImpute(full); for (int iter = 0; iter < maxIter; iter++) { svdImpute(data, full); } for (int i = 0; i < data.length; i++) { System.arraycopy(full[i], 0, data[i], 0, data[i].length); } }
java
public void impute(double[][] data, int maxIter) throws MissingValueImputationException { if (maxIter < 1) { throw new IllegalArgumentException("Invalid maximum number of iterations: " + maxIter); } int[] count = new int[data[0].length]; for (int i = 0; i < data.length; i++) { int n = 0; for (int j = 0; j < data[i].length; j++) { if (Double.isNaN(data[i][j])) { n++; count[j]++; } } if (n == data[i].length) { throw new MissingValueImputationException("The whole row " + i + " is missing"); } } for (int i = 0; i < data[0].length; i++) { if (count[i] == data.length) { throw new MissingValueImputationException("The whole column " + i + " is missing"); } } double[][] full = new double[data.length][]; for (int i = 0; i < full.length; i++) { full[i] = data[i].clone(); } KMeansImputation.columnAverageImpute(full); for (int iter = 0; iter < maxIter; iter++) { svdImpute(data, full); } for (int i = 0; i < data.length; i++) { System.arraycopy(full[i], 0, data[i], 0, data[i].length); } }
[ "public", "void", "impute", "(", "double", "[", "]", "[", "]", "data", ",", "int", "maxIter", ")", "throws", "MissingValueImputationException", "{", "if", "(", "maxIter", "<", "1", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Invalid maximum number of iterations: \"", "+", "maxIter", ")", ";", "}", "int", "[", "]", "count", "=", "new", "int", "[", "data", "[", "0", "]", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "data", ".", "length", ";", "i", "++", ")", "{", "int", "n", "=", "0", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "data", "[", "i", "]", ".", "length", ";", "j", "++", ")", "{", "if", "(", "Double", ".", "isNaN", "(", "data", "[", "i", "]", "[", "j", "]", ")", ")", "{", "n", "++", ";", "count", "[", "j", "]", "++", ";", "}", "}", "if", "(", "n", "==", "data", "[", "i", "]", ".", "length", ")", "{", "throw", "new", "MissingValueImputationException", "(", "\"The whole row \"", "+", "i", "+", "\" is missing\"", ")", ";", "}", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "data", "[", "0", "]", ".", "length", ";", "i", "++", ")", "{", "if", "(", "count", "[", "i", "]", "==", "data", ".", "length", ")", "{", "throw", "new", "MissingValueImputationException", "(", "\"The whole column \"", "+", "i", "+", "\" is missing\"", ")", ";", "}", "}", "double", "[", "]", "[", "]", "full", "=", "new", "double", "[", "data", ".", "length", "]", "[", "", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "full", ".", "length", ";", "i", "++", ")", "{", "full", "[", "i", "]", "=", "data", "[", "i", "]", ".", "clone", "(", ")", ";", "}", "KMeansImputation", ".", "columnAverageImpute", "(", "full", ")", ";", "for", "(", "int", "iter", "=", "0", ";", "iter", "<", "maxIter", ";", "iter", "++", ")", "{", "svdImpute", "(", "data", ",", "full", ")", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "data", ".", "length", ";", "i", "++", ")", "{", "System", ".", "arraycopy", "(", "full", "[", "i", "]", ",", "0", ",", "data", "[", "i", "]", ",", "0", ",", "data", "[", "i", "]", ".", "length", ")", ";", "}", "}" ]
Impute missing values in the dataset. @param data a data set with missing values (represented as Double.NaN). On output, missing values are filled with estimated values. @param maxIter the maximum number of iterations.
[ "Impute", "missing", "values", "in", "the", "dataset", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/imputation/SVDImputation.java#L75-L115
17,421
haifengl/smile
core/src/main/java/smile/imputation/SVDImputation.java
SVDImputation.svdImpute
private void svdImpute(double[][] raw, double[][] data) { SVD svd = Matrix.newInstance(data).svd(); int d = data[0].length; for (int i = 0; i < raw.length; i++) { int missing = 0; for (int j = 0; j < d; j++) { if (Double.isNaN(raw[i][j])) { missing++; } else { data[i][j] = raw[i][j]; } } if (missing == 0) { continue; } DenseMatrix A = Matrix.zeros(d - missing, k); double[] b = new double[d - missing]; for (int j = 0, m = 0; j < d; j++) { if (!Double.isNaN(raw[i][j])) { for (int l = 0; l < k; l++) { A.set(m, l, svd.getV().get(j, l)); } b[m++] = raw[i][j]; } } double[] s = new double[k]; QR qr = A.qr(); qr.solve(b, s); for (int j = 0; j < d; j++) { if (Double.isNaN(raw[i][j])) { data[i][j] = 0; for (int l = 0; l < k; l++) { data[i][j] += s[l] * svd.getV().get(j, l); } } } } }
java
private void svdImpute(double[][] raw, double[][] data) { SVD svd = Matrix.newInstance(data).svd(); int d = data[0].length; for (int i = 0; i < raw.length; i++) { int missing = 0; for (int j = 0; j < d; j++) { if (Double.isNaN(raw[i][j])) { missing++; } else { data[i][j] = raw[i][j]; } } if (missing == 0) { continue; } DenseMatrix A = Matrix.zeros(d - missing, k); double[] b = new double[d - missing]; for (int j = 0, m = 0; j < d; j++) { if (!Double.isNaN(raw[i][j])) { for (int l = 0; l < k; l++) { A.set(m, l, svd.getV().get(j, l)); } b[m++] = raw[i][j]; } } double[] s = new double[k]; QR qr = A.qr(); qr.solve(b, s); for (int j = 0; j < d; j++) { if (Double.isNaN(raw[i][j])) { data[i][j] = 0; for (int l = 0; l < k; l++) { data[i][j] += s[l] * svd.getV().get(j, l); } } } } }
[ "private", "void", "svdImpute", "(", "double", "[", "]", "[", "]", "raw", ",", "double", "[", "]", "[", "]", "data", ")", "{", "SVD", "svd", "=", "Matrix", ".", "newInstance", "(", "data", ")", ".", "svd", "(", ")", ";", "int", "d", "=", "data", "[", "0", "]", ".", "length", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "raw", ".", "length", ";", "i", "++", ")", "{", "int", "missing", "=", "0", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "d", ";", "j", "++", ")", "{", "if", "(", "Double", ".", "isNaN", "(", "raw", "[", "i", "]", "[", "j", "]", ")", ")", "{", "missing", "++", ";", "}", "else", "{", "data", "[", "i", "]", "[", "j", "]", "=", "raw", "[", "i", "]", "[", "j", "]", ";", "}", "}", "if", "(", "missing", "==", "0", ")", "{", "continue", ";", "}", "DenseMatrix", "A", "=", "Matrix", ".", "zeros", "(", "d", "-", "missing", ",", "k", ")", ";", "double", "[", "]", "b", "=", "new", "double", "[", "d", "-", "missing", "]", ";", "for", "(", "int", "j", "=", "0", ",", "m", "=", "0", ";", "j", "<", "d", ";", "j", "++", ")", "{", "if", "(", "!", "Double", ".", "isNaN", "(", "raw", "[", "i", "]", "[", "j", "]", ")", ")", "{", "for", "(", "int", "l", "=", "0", ";", "l", "<", "k", ";", "l", "++", ")", "{", "A", ".", "set", "(", "m", ",", "l", ",", "svd", ".", "getV", "(", ")", ".", "get", "(", "j", ",", "l", ")", ")", ";", "}", "b", "[", "m", "++", "]", "=", "raw", "[", "i", "]", "[", "j", "]", ";", "}", "}", "double", "[", "]", "s", "=", "new", "double", "[", "k", "]", ";", "QR", "qr", "=", "A", ".", "qr", "(", ")", ";", "qr", ".", "solve", "(", "b", ",", "s", ")", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "d", ";", "j", "++", ")", "{", "if", "(", "Double", ".", "isNaN", "(", "raw", "[", "i", "]", "[", "j", "]", ")", ")", "{", "data", "[", "i", "]", "[", "j", "]", "=", "0", ";", "for", "(", "int", "l", "=", "0", ";", "l", "<", "k", ";", "l", "++", ")", "{", "data", "[", "i", "]", "[", "j", "]", "+=", "s", "[", "l", "]", "*", "svd", ".", "getV", "(", ")", ".", "get", "(", "j", ",", "l", ")", ";", "}", "}", "}", "}", "}" ]
Impute the missing values by SVD. @param raw the raw data with missing values. @param data the data with current imputations.
[ "Impute", "the", "missing", "values", "by", "SVD", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/imputation/SVDImputation.java#L122-L166
17,422
haifengl/smile
interpolation/src/main/java/smile/interpolation/BicubicInterpolation.java
BicubicInterpolation.bcuint
private static double bcuint(double[] y, double[] y1, double[] y2, double[] y12, double x1l, double x1u, double x2l, double x2u, double x1p, double x2p) { if (x1u == x1l) { throw new IllegalArgumentException("Nearby control points take same value: " + x1u); } if (x2u == x2l) { throw new IllegalArgumentException("Nearby control points take same value: " + x2u); } double t, u, d1 = x1u - x1l, d2 = x2u - x2l; double[][] c = bcucof(y, y1, y2, y12, d1, d2); t = (x1p - x1l) / d1; u = (x2p - x2l) / d2; double ansy = 0.0; for (int i = 3; i >= 0; i--) { ansy = t * ansy + ((c[i][3] * u + c[i][2]) * u + c[i][1]) * u + c[i][0]; } return ansy; }
java
private static double bcuint(double[] y, double[] y1, double[] y2, double[] y12, double x1l, double x1u, double x2l, double x2u, double x1p, double x2p) { if (x1u == x1l) { throw new IllegalArgumentException("Nearby control points take same value: " + x1u); } if (x2u == x2l) { throw new IllegalArgumentException("Nearby control points take same value: " + x2u); } double t, u, d1 = x1u - x1l, d2 = x2u - x2l; double[][] c = bcucof(y, y1, y2, y12, d1, d2); t = (x1p - x1l) / d1; u = (x2p - x2l) / d2; double ansy = 0.0; for (int i = 3; i >= 0; i--) { ansy = t * ansy + ((c[i][3] * u + c[i][2]) * u + c[i][1]) * u + c[i][0]; } return ansy; }
[ "private", "static", "double", "bcuint", "(", "double", "[", "]", "y", ",", "double", "[", "]", "y1", ",", "double", "[", "]", "y2", ",", "double", "[", "]", "y12", ",", "double", "x1l", ",", "double", "x1u", ",", "double", "x2l", ",", "double", "x2u", ",", "double", "x1p", ",", "double", "x2p", ")", "{", "if", "(", "x1u", "==", "x1l", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Nearby control points take same value: \"", "+", "x1u", ")", ";", "}", "if", "(", "x2u", "==", "x2l", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Nearby control points take same value: \"", "+", "x2u", ")", ";", "}", "double", "t", ",", "u", ",", "d1", "=", "x1u", "-", "x1l", ",", "d2", "=", "x2u", "-", "x2l", ";", "double", "[", "]", "[", "]", "c", "=", "bcucof", "(", "y", ",", "y1", ",", "y2", ",", "y12", ",", "d1", ",", "d2", ")", ";", "t", "=", "(", "x1p", "-", "x1l", ")", "/", "d1", ";", "u", "=", "(", "x2p", "-", "x2l", ")", "/", "d2", ";", "double", "ansy", "=", "0.0", ";", "for", "(", "int", "i", "=", "3", ";", "i", ">=", "0", ";", "i", "--", ")", "{", "ansy", "=", "t", "*", "ansy", "+", "(", "(", "c", "[", "i", "]", "[", "3", "]", "*", "u", "+", "c", "[", "i", "]", "[", "2", "]", ")", "*", "u", "+", "c", "[", "i", "]", "[", "1", "]", ")", "*", "u", "+", "c", "[", "i", "]", "[", "0", "]", ";", "}", "return", "ansy", ";", "}" ]
Bicubic interpolation within a grid square.
[ "Bicubic", "interpolation", "within", "a", "grid", "square", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/interpolation/src/main/java/smile/interpolation/BicubicInterpolation.java#L132-L156
17,423
haifengl/smile
plot/src/main/java/smile/plot/Wireframe.java
Wireframe.plot
public static PlotCanvas plot(double[][] vertices, int[][] edges) { double[] lowerBound = Math.colMin(vertices); double[] upperBound = Math.colMax(vertices); PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound); Wireframe frame = new Wireframe(vertices, edges); canvas.add(frame); return canvas; }
java
public static PlotCanvas plot(double[][] vertices, int[][] edges) { double[] lowerBound = Math.colMin(vertices); double[] upperBound = Math.colMax(vertices); PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound); Wireframe frame = new Wireframe(vertices, edges); canvas.add(frame); return canvas; }
[ "public", "static", "PlotCanvas", "plot", "(", "double", "[", "]", "[", "]", "vertices", ",", "int", "[", "]", "[", "]", "edges", ")", "{", "double", "[", "]", "lowerBound", "=", "Math", ".", "colMin", "(", "vertices", ")", ";", "double", "[", "]", "upperBound", "=", "Math", ".", "colMax", "(", "vertices", ")", ";", "PlotCanvas", "canvas", "=", "new", "PlotCanvas", "(", "lowerBound", ",", "upperBound", ")", ";", "Wireframe", "frame", "=", "new", "Wireframe", "(", "vertices", ",", "edges", ")", ";", "canvas", ".", "add", "(", "frame", ")", ";", "return", "canvas", ";", "}" ]
Create a wire frame plot canvas. @param vertices a n-by-2 or n-by-3 array which are coordinates of n vertices.
[ "Create", "a", "wire", "frame", "plot", "canvas", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Wireframe.java#L78-L88
17,424
haifengl/smile
core/src/main/java/smile/feature/SparseOneHotEncoder.java
SparseOneHotEncoder.feature
public int[] feature(double[] x) { if (x.length != attributes.length) { throw new IllegalArgumentException(String.format("Invalid feature vector size %d, expected %d", x.length, attributes.length)); } int[] features = new int[attributes.length]; for (int i = 0; i < features.length; i++) { int f = (int) x[i]; if (Math.floor(x[i]) != x[i] || f < 0 || f >= attributes[i].size()) { throw new IllegalArgumentException(String.format("Invalid value of attribute %s: %d", attributes[i].toString(), f)); } features[i] = f + base[i]; } return features; }
java
public int[] feature(double[] x) { if (x.length != attributes.length) { throw new IllegalArgumentException(String.format("Invalid feature vector size %d, expected %d", x.length, attributes.length)); } int[] features = new int[attributes.length]; for (int i = 0; i < features.length; i++) { int f = (int) x[i]; if (Math.floor(x[i]) != x[i] || f < 0 || f >= attributes[i].size()) { throw new IllegalArgumentException(String.format("Invalid value of attribute %s: %d", attributes[i].toString(), f)); } features[i] = f + base[i]; } return features; }
[ "public", "int", "[", "]", "feature", "(", "double", "[", "]", "x", ")", "{", "if", "(", "x", ".", "length", "!=", "attributes", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"Invalid feature vector size %d, expected %d\"", ",", "x", ".", "length", ",", "attributes", ".", "length", ")", ")", ";", "}", "int", "[", "]", "features", "=", "new", "int", "[", "attributes", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "features", ".", "length", ";", "i", "++", ")", "{", "int", "f", "=", "(", "int", ")", "x", "[", "i", "]", ";", "if", "(", "Math", ".", "floor", "(", "x", "[", "i", "]", ")", "!=", "x", "[", "i", "]", "||", "f", "<", "0", "||", "f", ">=", "attributes", "[", "i", "]", ".", "size", "(", ")", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"Invalid value of attribute %s: %d\"", ",", "attributes", "[", "i", "]", ".", "toString", "(", ")", ",", "f", ")", ")", ";", "}", "features", "[", "i", "]", "=", "f", "+", "base", "[", "i", "]", ";", "}", "return", "features", ";", "}" ]
Generates the compact representation of sparse binary features for given object. @param x an object of interest. @return an integer array of nonzero binary features.
[ "Generates", "the", "compact", "representation", "of", "sparse", "binary", "features", "for", "given", "object", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/feature/SparseOneHotEncoder.java#L69-L85
17,425
haifengl/smile
symbolic/src/main/java/smile/symbolic/ExpressionParser.java
ExpressionParser.format
public String format(String s) { for(int i = 0; i < s.length(); i++) { if(s.substring(i, i + 1).equals("*") && i > 0) if(isOperand(s.substring(i - 1, i), var) && i < s.length() - 1 && s.substring(i + 1, i + 2).equals(var)) s = s.substring(0, i) + s.substring(i + 1); } return s; }
java
public String format(String s) { for(int i = 0; i < s.length(); i++) { if(s.substring(i, i + 1).equals("*") && i > 0) if(isOperand(s.substring(i - 1, i), var) && i < s.length() - 1 && s.substring(i + 1, i + 2).equals(var)) s = s.substring(0, i) + s.substring(i + 1); } return s; }
[ "public", "String", "format", "(", "String", "s", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "s", ".", "length", "(", ")", ";", "i", "++", ")", "{", "if", "(", "s", ".", "substring", "(", "i", ",", "i", "+", "1", ")", ".", "equals", "(", "\"*\"", ")", "&&", "i", ">", "0", ")", "if", "(", "isOperand", "(", "s", ".", "substring", "(", "i", "-", "1", ",", "i", ")", ",", "var", ")", "&&", "i", "<", "s", ".", "length", "(", ")", "-", "1", "&&", "s", ".", "substring", "(", "i", "+", "1", ",", "i", "+", "2", ")", ".", "equals", "(", "var", ")", ")", "s", "=", "s", ".", "substring", "(", "0", ",", "i", ")", "+", "s", ".", "substring", "(", "i", "+", "1", ")", ";", "}", "return", "s", ";", "}" ]
Modifies the string to look more like it would if someone wrote the expression out on paper. @param s the mathematical expression.
[ "Modifies", "the", "string", "to", "look", "more", "like", "it", "would", "if", "someone", "wrote", "the", "expression", "out", "on", "paper", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/symbolic/src/main/java/smile/symbolic/ExpressionParser.java#L104-L113
17,426
haifengl/smile
symbolic/src/main/java/smile/symbolic/ExpressionParser.java
ExpressionParser.parse
public String parse(String expression) throws InvalidExpressionException { this.expression = expression; try { var = check(); } catch (InvalidExpressionException e) { e.printStackTrace(); } this.expression = formatString(expression); System.out.println(this.expression); tokens = tokenize(this.expression); return this.expression; }
java
public String parse(String expression) throws InvalidExpressionException { this.expression = expression; try { var = check(); } catch (InvalidExpressionException e) { e.printStackTrace(); } this.expression = formatString(expression); System.out.println(this.expression); tokens = tokenize(this.expression); return this.expression; }
[ "public", "String", "parse", "(", "String", "expression", ")", "throws", "InvalidExpressionException", "{", "this", ".", "expression", "=", "expression", ";", "try", "{", "var", "=", "check", "(", ")", ";", "}", "catch", "(", "InvalidExpressionException", "e", ")", "{", "e", ".", "printStackTrace", "(", ")", ";", "}", "this", ".", "expression", "=", "formatString", "(", "expression", ")", ";", "System", ".", "out", ".", "println", "(", "this", ".", "expression", ")", ";", "tokens", "=", "tokenize", "(", "this", ".", "expression", ")", ";", "return", "this", ".", "expression", ";", "}" ]
Creates tokens in polish notation for and re-formats the expression, unless an expression without valid syntax is passed in. @param expression a mathematical expression. @return a modified version of the expression.
[ "Creates", "tokens", "in", "polish", "notation", "for", "and", "re", "-", "formats", "the", "expression", "unless", "an", "expression", "without", "valid", "syntax", "is", "passed", "in", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/symbolic/src/main/java/smile/symbolic/ExpressionParser.java#L121-L136
17,427
haifengl/smile
symbolic/src/main/java/smile/symbolic/ExpressionParser.java
ExpressionParser.formatString
private String formatString(String exp) { exp = exp.replaceAll("\\s",""); // why exp = exp.toLowerCase(); int count = 0; if(exp.substring(0, 1).equals("-")) { // if expression starts with a minus sign, it is a unary one exp = "$" + exp.substring(1); // replace } for(int i = 0; i < exp.length(); i++) { if(exp.substring(i, i + 1).equals("(")) count++; else if(exp.substring(i, i + 1).equals(")")) count--; } while(count > 0) { exp += ")"; count--; } // At the operators, when the operator is "-" and it is preceded by another operator, // or preceded by a left parenthesis, or when it is the first character of the input // it is a unary minus rather than binary. In this case, I change it to another // character, '$', and make its precedence the same as that of '^'. for(int i = 0; i < exp.length() - 1; i++) { String tmp1 = exp.substring(i, i + 1); String tmp2 = exp.substring(i + 1, i + 2); if(tmp2.equals("-") && (ExpressionParser.isOperator(tmp1) || tmp1.equals("("))) exp = exp.substring(0, i + 1) + "$" + exp.substring(i + 2); else if((tmp1.matches("[0-9]+") || tmp1.equals(var)) && (tmp2.equals("(") || tmp2.equals(var))) exp = exp.substring(0, i + 1) + "*" + exp.substring(i + 1); } return exp; }
java
private String formatString(String exp) { exp = exp.replaceAll("\\s",""); // why exp = exp.toLowerCase(); int count = 0; if(exp.substring(0, 1).equals("-")) { // if expression starts with a minus sign, it is a unary one exp = "$" + exp.substring(1); // replace } for(int i = 0; i < exp.length(); i++) { if(exp.substring(i, i + 1).equals("(")) count++; else if(exp.substring(i, i + 1).equals(")")) count--; } while(count > 0) { exp += ")"; count--; } // At the operators, when the operator is "-" and it is preceded by another operator, // or preceded by a left parenthesis, or when it is the first character of the input // it is a unary minus rather than binary. In this case, I change it to another // character, '$', and make its precedence the same as that of '^'. for(int i = 0; i < exp.length() - 1; i++) { String tmp1 = exp.substring(i, i + 1); String tmp2 = exp.substring(i + 1, i + 2); if(tmp2.equals("-") && (ExpressionParser.isOperator(tmp1) || tmp1.equals("("))) exp = exp.substring(0, i + 1) + "$" + exp.substring(i + 2); else if((tmp1.matches("[0-9]+") || tmp1.equals(var)) && (tmp2.equals("(") || tmp2.equals(var))) exp = exp.substring(0, i + 1) + "*" + exp.substring(i + 1); } return exp; }
[ "private", "String", "formatString", "(", "String", "exp", ")", "{", "exp", "=", "exp", ".", "replaceAll", "(", "\"\\\\s\"", ",", "\"\"", ")", ";", "// why", "exp", "=", "exp", ".", "toLowerCase", "(", ")", ";", "int", "count", "=", "0", ";", "if", "(", "exp", ".", "substring", "(", "0", ",", "1", ")", ".", "equals", "(", "\"-\"", ")", ")", "{", "// if expression starts with a minus sign, it is a unary one", "exp", "=", "\"$\"", "+", "exp", ".", "substring", "(", "1", ")", ";", "// replace", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "exp", ".", "length", "(", ")", ";", "i", "++", ")", "{", "if", "(", "exp", ".", "substring", "(", "i", ",", "i", "+", "1", ")", ".", "equals", "(", "\"(\"", ")", ")", "count", "++", ";", "else", "if", "(", "exp", ".", "substring", "(", "i", ",", "i", "+", "1", ")", ".", "equals", "(", "\")\"", ")", ")", "count", "--", ";", "}", "while", "(", "count", ">", "0", ")", "{", "exp", "+=", "\")\"", ";", "count", "--", ";", "}", "// At the operators, when the operator is \"-\" and it is preceded by another operator,", "// or preceded by a left parenthesis, or when it is the first character of the input", "// it is a unary minus rather than binary. In this case, I change it to another", "// character, '$', and make its precedence the same as that of '^'.", "for", "(", "int", "i", "=", "0", ";", "i", "<", "exp", ".", "length", "(", ")", "-", "1", ";", "i", "++", ")", "{", "String", "tmp1", "=", "exp", ".", "substring", "(", "i", ",", "i", "+", "1", ")", ";", "String", "tmp2", "=", "exp", ".", "substring", "(", "i", "+", "1", ",", "i", "+", "2", ")", ";", "if", "(", "tmp2", ".", "equals", "(", "\"-\"", ")", "&&", "(", "ExpressionParser", ".", "isOperator", "(", "tmp1", ")", "||", "tmp1", ".", "equals", "(", "\"(\"", ")", ")", ")", "exp", "=", "exp", ".", "substring", "(", "0", ",", "i", "+", "1", ")", "+", "\"$\"", "+", "exp", ".", "substring", "(", "i", "+", "2", ")", ";", "else", "if", "(", "(", "tmp1", ".", "matches", "(", "\"[0-9]+\"", ")", "||", "tmp1", ".", "equals", "(", "var", ")", ")", "&&", "(", "tmp2", ".", "equals", "(", "\"(\"", ")", "||", "tmp2", ".", "equals", "(", "var", ")", ")", ")", "exp", "=", "exp", ".", "substring", "(", "0", ",", "i", "+", "1", ")", "+", "\"*\"", "+", "exp", ".", "substring", "(", "i", "+", "1", ")", ";", "}", "return", "exp", ";", "}" ]
adds and deletes characters to aid in the creation of the binary expression tree
[ "adds", "and", "deletes", "characters", "to", "aid", "in", "the", "creation", "of", "the", "binary", "expression", "tree" ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/symbolic/src/main/java/smile/symbolic/ExpressionParser.java#L249-L285
17,428
haifengl/smile
netlib/src/main/java/smile/netlib/NLMatrix.java
NLMatrix.reverse
static void reverse(double[] d, DenseMatrix V) { int m = V.nrows(); int n = d.length; int half = n / 2; for (int i = 0; i < half; i++) { double tmp = d[i]; d[i] = d[n - i - 1]; d[n - i - 1] = tmp; } for (int j = 0; j < half; j++) { for (int i = 0; i < m; i++) { double tmp = V.get(i, j); V.set(i, j, V.get(i, n - j - 1)); V.set(i, n - j - 1, tmp); } } }
java
static void reverse(double[] d, DenseMatrix V) { int m = V.nrows(); int n = d.length; int half = n / 2; for (int i = 0; i < half; i++) { double tmp = d[i]; d[i] = d[n - i - 1]; d[n - i - 1] = tmp; } for (int j = 0; j < half; j++) { for (int i = 0; i < m; i++) { double tmp = V.get(i, j); V.set(i, j, V.get(i, n - j - 1)); V.set(i, n - j - 1, tmp); } } }
[ "static", "void", "reverse", "(", "double", "[", "]", "d", ",", "DenseMatrix", "V", ")", "{", "int", "m", "=", "V", ".", "nrows", "(", ")", ";", "int", "n", "=", "d", ".", "length", ";", "int", "half", "=", "n", "/", "2", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "half", ";", "i", "++", ")", "{", "double", "tmp", "=", "d", "[", "i", "]", ";", "d", "[", "i", "]", "=", "d", "[", "n", "-", "i", "-", "1", "]", ";", "d", "[", "n", "-", "i", "-", "1", "]", "=", "tmp", ";", "}", "for", "(", "int", "j", "=", "0", ";", "j", "<", "half", ";", "j", "++", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "m", ";", "i", "++", ")", "{", "double", "tmp", "=", "V", ".", "get", "(", "i", ",", "j", ")", ";", "V", ".", "set", "(", "i", ",", "j", ",", "V", ".", "get", "(", "i", ",", "n", "-", "j", "-", "1", ")", ")", ";", "V", ".", "set", "(", "i", ",", "n", "-", "j", "-", "1", ",", "tmp", ")", ";", "}", "}", "}" ]
Reverse the array to match JMatrix.
[ "Reverse", "the", "array", "to", "match", "JMatrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/netlib/src/main/java/smile/netlib/NLMatrix.java#L516-L532
17,429
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.logistic
public static double logistic(double x) { double y = 0.0; if (x < -40) { y = 2.353853e+17; } else if (x > 40) { y = 1.0 + 4.248354e-18; } else { y = 1.0 + Math.exp(-x); } return 1.0 / y; }
java
public static double logistic(double x) { double y = 0.0; if (x < -40) { y = 2.353853e+17; } else if (x > 40) { y = 1.0 + 4.248354e-18; } else { y = 1.0 + Math.exp(-x); } return 1.0 / y; }
[ "public", "static", "double", "logistic", "(", "double", "x", ")", "{", "double", "y", "=", "0.0", ";", "if", "(", "x", "<", "-", "40", ")", "{", "y", "=", "2.353853e+17", ";", "}", "else", "if", "(", "x", ">", "40", ")", "{", "y", "=", "1.0", "+", "4.248354e-18", ";", "}", "else", "{", "y", "=", "1.0", "+", "Math", ".", "exp", "(", "-", "x", ")", ";", "}", "return", "1.0", "/", "y", ";", "}" ]
Logistic sigmoid function.
[ "Logistic", "sigmoid", "function", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L632-L643
17,430
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.round
public static double round(double x, int decimal) { if (decimal < 0) { return round(x / pow(10, -decimal)) * pow(10, -decimal); } else { return round(x * pow(10, decimal)) / pow(10, decimal); } }
java
public static double round(double x, int decimal) { if (decimal < 0) { return round(x / pow(10, -decimal)) * pow(10, -decimal); } else { return round(x * pow(10, decimal)) / pow(10, decimal); } }
[ "public", "static", "double", "round", "(", "double", "x", ",", "int", "decimal", ")", "{", "if", "(", "decimal", "<", "0", ")", "{", "return", "round", "(", "x", "/", "pow", "(", "10", ",", "-", "decimal", ")", ")", "*", "pow", "(", "10", ",", "-", "decimal", ")", ";", "}", "else", "{", "return", "round", "(", "x", "*", "pow", "(", "10", ",", "decimal", ")", ")", "/", "pow", "(", "10", ",", "decimal", ")", ";", "}", "}" ]
Round a double vale to given digits such as 10^n, where n is a positive or negative integer.
[ "Round", "a", "double", "vale", "to", "given", "digits", "such", "as", "10^n", "where", "n", "is", "a", "positive", "or", "negative", "integer", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L663-L669
17,431
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.logFactorial
public static double logFactorial(int n) { if (n < 0) { throw new IllegalArgumentException(String.format("n has to be nonnegative: %d", n)); } double f = 0.0; for (int i = 2; i <= n; i++) { f += Math.log(i); } return f; }
java
public static double logFactorial(int n) { if (n < 0) { throw new IllegalArgumentException(String.format("n has to be nonnegative: %d", n)); } double f = 0.0; for (int i = 2; i <= n; i++) { f += Math.log(i); } return f; }
[ "public", "static", "double", "logFactorial", "(", "int", "n", ")", "{", "if", "(", "n", "<", "0", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"n has to be nonnegative: %d\"", ",", "n", ")", ")", ";", "}", "double", "f", "=", "0.0", ";", "for", "(", "int", "i", "=", "2", ";", "i", "<=", "n", ";", "i", "++", ")", "{", "f", "+=", "Math", ".", "log", "(", "i", ")", ";", "}", "return", "f", ";", "}" ]
log of factorial of n
[ "log", "of", "factorial", "of", "n" ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L693-L704
17,432
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.choose
public static double choose(int n, int k) { if (n < 0 || k < 0) { throw new IllegalArgumentException(String.format("Invalid n = %d, k = %d", n, k)); } if (n < k) { return 0.0; } return Math.floor(0.5 + Math.exp(logChoose(n, k))); }
java
public static double choose(int n, int k) { if (n < 0 || k < 0) { throw new IllegalArgumentException(String.format("Invalid n = %d, k = %d", n, k)); } if (n < k) { return 0.0; } return Math.floor(0.5 + Math.exp(logChoose(n, k))); }
[ "public", "static", "double", "choose", "(", "int", "n", ",", "int", "k", ")", "{", "if", "(", "n", "<", "0", "||", "k", "<", "0", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"Invalid n = %d, k = %d\"", ",", "n", ",", "k", ")", ")", ";", "}", "if", "(", "n", "<", "k", ")", "{", "return", "0.0", ";", "}", "return", "Math", ".", "floor", "(", "0.5", "+", "Math", ".", "exp", "(", "logChoose", "(", "n", ",", "k", ")", ")", ")", ";", "}" ]
n choose k. Returns 0 if n is less than k.
[ "n", "choose", "k", ".", "Returns", "0", "if", "n", "is", "less", "than", "k", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L709-L719
17,433
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.logChoose
public static double logChoose(int n, int k) { if (n < 0 || k < 0 || k > n) { throw new IllegalArgumentException(String.format("Invalid n = %d, k = %d", n, k)); } return Math.logFactorial(n) - Math.logFactorial(k) - Math.logFactorial(n - k); }
java
public static double logChoose(int n, int k) { if (n < 0 || k < 0 || k > n) { throw new IllegalArgumentException(String.format("Invalid n = %d, k = %d", n, k)); } return Math.logFactorial(n) - Math.logFactorial(k) - Math.logFactorial(n - k); }
[ "public", "static", "double", "logChoose", "(", "int", "n", ",", "int", "k", ")", "{", "if", "(", "n", "<", "0", "||", "k", "<", "0", "||", "k", ">", "n", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"Invalid n = %d, k = %d\"", ",", "n", ",", "k", ")", ")", ";", "}", "return", "Math", ".", "logFactorial", "(", "n", ")", "-", "Math", ".", "logFactorial", "(", "k", ")", "-", "Math", ".", "logFactorial", "(", "n", "-", "k", ")", ";", "}" ]
log of n choose k
[ "log", "of", "n", "choose", "k" ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L724-L730
17,434
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.random
public static int[] random(double[] prob, int n) { // set up alias table double[] q = new double[prob.length]; for (int i = 0; i < prob.length; i++) { q[i] = prob[i] * prob.length; } // initialize a with indices int[] a = new int[prob.length]; for (int i = 0; i < prob.length; i++) { a[i] = i; } // set up H and L int[] HL = new int[prob.length]; int head = 0; int tail = prob.length - 1; for (int i = 0; i < prob.length; i++) { if (q[i] >= 1.0) { HL[head++] = i; } else { HL[tail--] = i; } } while (head != 0 && tail != prob.length - 1) { int j = HL[tail + 1]; int k = HL[head - 1]; a[j] = k; q[k] += q[j] - 1; tail++; // remove j from L if (q[k] < 1.0) { HL[tail--] = k; // add k to L head--; // remove k } } // generate sample int[] ans = new int[n]; for (int i = 0; i < n; i++) { double rU = random() * prob.length; int k = (int) (rU); rU -= k; /* rU becomes rU-[rU] */ if (rU < q[k]) { ans[i] = k; } else { ans[i] = a[k]; } } return ans; }
java
public static int[] random(double[] prob, int n) { // set up alias table double[] q = new double[prob.length]; for (int i = 0; i < prob.length; i++) { q[i] = prob[i] * prob.length; } // initialize a with indices int[] a = new int[prob.length]; for (int i = 0; i < prob.length; i++) { a[i] = i; } // set up H and L int[] HL = new int[prob.length]; int head = 0; int tail = prob.length - 1; for (int i = 0; i < prob.length; i++) { if (q[i] >= 1.0) { HL[head++] = i; } else { HL[tail--] = i; } } while (head != 0 && tail != prob.length - 1) { int j = HL[tail + 1]; int k = HL[head - 1]; a[j] = k; q[k] += q[j] - 1; tail++; // remove j from L if (q[k] < 1.0) { HL[tail--] = k; // add k to L head--; // remove k } } // generate sample int[] ans = new int[n]; for (int i = 0; i < n; i++) { double rU = random() * prob.length; int k = (int) (rU); rU -= k; /* rU becomes rU-[rU] */ if (rU < q[k]) { ans[i] = k; } else { ans[i] = a[k]; } } return ans; }
[ "public", "static", "int", "[", "]", "random", "(", "double", "[", "]", "prob", ",", "int", "n", ")", "{", "// set up alias table", "double", "[", "]", "q", "=", "new", "double", "[", "prob", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "prob", ".", "length", ";", "i", "++", ")", "{", "q", "[", "i", "]", "=", "prob", "[", "i", "]", "*", "prob", ".", "length", ";", "}", "// initialize a with indices", "int", "[", "]", "a", "=", "new", "int", "[", "prob", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "prob", ".", "length", ";", "i", "++", ")", "{", "a", "[", "i", "]", "=", "i", ";", "}", "// set up H and L", "int", "[", "]", "HL", "=", "new", "int", "[", "prob", ".", "length", "]", ";", "int", "head", "=", "0", ";", "int", "tail", "=", "prob", ".", "length", "-", "1", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "prob", ".", "length", ";", "i", "++", ")", "{", "if", "(", "q", "[", "i", "]", ">=", "1.0", ")", "{", "HL", "[", "head", "++", "]", "=", "i", ";", "}", "else", "{", "HL", "[", "tail", "--", "]", "=", "i", ";", "}", "}", "while", "(", "head", "!=", "0", "&&", "tail", "!=", "prob", ".", "length", "-", "1", ")", "{", "int", "j", "=", "HL", "[", "tail", "+", "1", "]", ";", "int", "k", "=", "HL", "[", "head", "-", "1", "]", ";", "a", "[", "j", "]", "=", "k", ";", "q", "[", "k", "]", "+=", "q", "[", "j", "]", "-", "1", ";", "tail", "++", ";", "// remove j from L", "if", "(", "q", "[", "k", "]", "<", "1.0", ")", "{", "HL", "[", "tail", "--", "]", "=", "k", ";", "// add k to L", "head", "--", ";", "// remove k", "}", "}", "// generate sample", "int", "[", "]", "ans", "=", "new", "int", "[", "n", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "double", "rU", "=", "random", "(", ")", "*", "prob", ".", "length", ";", "int", "k", "=", "(", "int", ")", "(", "rU", ")", ";", "rU", "-=", "k", ";", "/* rU becomes rU-[rU] */", "if", "(", "rU", "<", "q", "[", "k", "]", ")", "{", "ans", "[", "i", "]", "=", "k", ";", "}", "else", "{", "ans", "[", "i", "]", "=", "a", "[", "k", "]", ";", "}", "}", "return", "ans", ";", "}" ]
Given a set of m probabilities, draw with replacement a set of n random number in [0, m). @param prob probabilities of size n. The prob argument can be used to give a vector of weights for obtaining the elements of the vector being sampled. They need not sum to one, but they should be nonnegative and not all zero. @return an random array of length n in range of [0, m).
[ "Given", "a", "set", "of", "m", "probabilities", "draw", "with", "replacement", "a", "set", "of", "n", "random", "number", "in", "[", "0", "m", ")", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L761-L814
17,435
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.random
public static double[] random(double lo, double hi, int n) { double[] x = new double[n]; random.get().nextDoubles(x, lo, hi); return x; }
java
public static double[] random(double lo, double hi, int n) { double[] x = new double[n]; random.get().nextDoubles(x, lo, hi); return x; }
[ "public", "static", "double", "[", "]", "random", "(", "double", "lo", ",", "double", "hi", ",", "int", "n", ")", "{", "double", "[", "]", "x", "=", "new", "double", "[", "n", "]", ";", "random", ".", "get", "(", ")", ".", "nextDoubles", "(", "x", ",", "lo", ",", "hi", ")", ";", "return", "x", ";", "}" ]
Generate n uniform random numbers in the range [lo, hi). @param n size of the array @param lo lower limit of range @param hi upper limit of range @return a uniform random real in the range [lo, hi)
[ "Generate", "n", "uniform", "random", "numbers", "in", "the", "range", "[", "lo", "hi", ")", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L849-L853
17,436
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.randomInt
public static int randomInt(int lo, int hi) { int w = hi - lo; return lo + random.get().nextInt(w); }
java
public static int randomInt(int lo, int hi) { int w = hi - lo; return lo + random.get().nextInt(w); }
[ "public", "static", "int", "randomInt", "(", "int", "lo", ",", "int", "hi", ")", "{", "int", "w", "=", "hi", "-", "lo", ";", "return", "lo", "+", "random", ".", "get", "(", ")", ".", "nextInt", "(", "w", ")", ";", "}" ]
Returns a random integer in [lo, hi).
[ "Returns", "a", "random", "integer", "in", "[", "lo", "hi", ")", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L865-L868
17,437
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.c
public static int[] c(int[]... x) { int n = 0; for (int i = 0; i < x.length; i++) { n += x.length; } int[] y = new int[n]; for (int i = 0, k = 0; i < x.length; i++) { for (int xi : x[i]) { y[k++] = xi; } } return y; }
java
public static int[] c(int[]... x) { int n = 0; for (int i = 0; i < x.length; i++) { n += x.length; } int[] y = new int[n]; for (int i = 0, k = 0; i < x.length; i++) { for (int xi : x[i]) { y[k++] = xi; } } return y; }
[ "public", "static", "int", "[", "]", "c", "(", "int", "[", "]", "...", "x", ")", "{", "int", "n", "=", "0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "n", "+=", "x", ".", "length", ";", "}", "int", "[", "]", "y", "=", "new", "int", "[", "n", "]", ";", "for", "(", "int", "i", "=", "0", ",", "k", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "for", "(", "int", "xi", ":", "x", "[", "i", "]", ")", "{", "y", "[", "k", "++", "]", "=", "xi", ";", "}", "}", "return", "y", ";", "}" ]
Merges multiple vectors into one.
[ "Merges", "multiple", "vectors", "into", "one", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L927-L940
17,438
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.contains
public static boolean contains(double[][] polygon, double x, double y) { if (polygon.length <= 2) { return false; } int hits = 0; int n = polygon.length; double lastx = polygon[n - 1][0]; double lasty = polygon[n - 1][1]; double curx, cury; // Walk the edges of the polygon for (int i = 0; i < n; lastx = curx, lasty = cury, i++) { curx = polygon[i][0]; cury = polygon[i][1]; if (cury == lasty) { continue; } double leftx; if (curx < lastx) { if (x >= lastx) { continue; } leftx = curx; } else { if (x >= curx) { continue; } leftx = lastx; } double test1, test2; if (cury < lasty) { if (y < cury || y >= lasty) { continue; } if (x < leftx) { hits++; continue; } test1 = x - curx; test2 = y - cury; } else { if (y < lasty || y >= cury) { continue; } if (x < leftx) { hits++; continue; } test1 = x - lastx; test2 = y - lasty; } if (test1 < (test2 / (lasty - cury) * (lastx - curx))) { hits++; } } return ((hits & 1) != 0); }
java
public static boolean contains(double[][] polygon, double x, double y) { if (polygon.length <= 2) { return false; } int hits = 0; int n = polygon.length; double lastx = polygon[n - 1][0]; double lasty = polygon[n - 1][1]; double curx, cury; // Walk the edges of the polygon for (int i = 0; i < n; lastx = curx, lasty = cury, i++) { curx = polygon[i][0]; cury = polygon[i][1]; if (cury == lasty) { continue; } double leftx; if (curx < lastx) { if (x >= lastx) { continue; } leftx = curx; } else { if (x >= curx) { continue; } leftx = lastx; } double test1, test2; if (cury < lasty) { if (y < cury || y >= lasty) { continue; } if (x < leftx) { hits++; continue; } test1 = x - curx; test2 = y - cury; } else { if (y < lasty || y >= cury) { continue; } if (x < leftx) { hits++; continue; } test1 = x - lastx; test2 = y - lasty; } if (test1 < (test2 / (lasty - cury) * (lastx - curx))) { hits++; } } return ((hits & 1) != 0); }
[ "public", "static", "boolean", "contains", "(", "double", "[", "]", "[", "]", "polygon", ",", "double", "x", ",", "double", "y", ")", "{", "if", "(", "polygon", ".", "length", "<=", "2", ")", "{", "return", "false", ";", "}", "int", "hits", "=", "0", ";", "int", "n", "=", "polygon", ".", "length", ";", "double", "lastx", "=", "polygon", "[", "n", "-", "1", "]", "[", "0", "]", ";", "double", "lasty", "=", "polygon", "[", "n", "-", "1", "]", "[", "1", "]", ";", "double", "curx", ",", "cury", ";", "// Walk the edges of the polygon", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "lastx", "=", "curx", ",", "lasty", "=", "cury", ",", "i", "++", ")", "{", "curx", "=", "polygon", "[", "i", "]", "[", "0", "]", ";", "cury", "=", "polygon", "[", "i", "]", "[", "1", "]", ";", "if", "(", "cury", "==", "lasty", ")", "{", "continue", ";", "}", "double", "leftx", ";", "if", "(", "curx", "<", "lastx", ")", "{", "if", "(", "x", ">=", "lastx", ")", "{", "continue", ";", "}", "leftx", "=", "curx", ";", "}", "else", "{", "if", "(", "x", ">=", "curx", ")", "{", "continue", ";", "}", "leftx", "=", "lastx", ";", "}", "double", "test1", ",", "test2", ";", "if", "(", "cury", "<", "lasty", ")", "{", "if", "(", "y", "<", "cury", "||", "y", ">=", "lasty", ")", "{", "continue", ";", "}", "if", "(", "x", "<", "leftx", ")", "{", "hits", "++", ";", "continue", ";", "}", "test1", "=", "x", "-", "curx", ";", "test2", "=", "y", "-", "cury", ";", "}", "else", "{", "if", "(", "y", "<", "lasty", "||", "y", ">=", "cury", ")", "{", "continue", ";", "}", "if", "(", "x", "<", "leftx", ")", "{", "hits", "++", ";", "continue", ";", "}", "test1", "=", "x", "-", "lastx", ";", "test2", "=", "y", "-", "lasty", ";", "}", "if", "(", "test1", "<", "(", "test2", "/", "(", "lasty", "-", "cury", ")", "*", "(", "lastx", "-", "curx", ")", ")", ")", "{", "hits", "++", ";", "}", "}", "return", "(", "(", "hits", "&", "1", ")", "!=", "0", ")", ";", "}" ]
Determines if the polygon contains the specified coordinates. @param x the specified x coordinate. @param y the specified y coordinate. @return true if the Polygon contains the specified coordinates; false otherwise.
[ "Determines", "if", "the", "polygon", "contains", "the", "specified", "coordinates", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1102-L1165
17,439
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.reverse
public static void reverse(int[] a) { int i = 0, j = a.length - 1; while (i < j) { SortUtils.swap(a, i++, j--); // code for swap not shown, but easy enough } }
java
public static void reverse(int[] a) { int i = 0, j = a.length - 1; while (i < j) { SortUtils.swap(a, i++, j--); // code for swap not shown, but easy enough } }
[ "public", "static", "void", "reverse", "(", "int", "[", "]", "a", ")", "{", "int", "i", "=", "0", ",", "j", "=", "a", ".", "length", "-", "1", ";", "while", "(", "i", "<", "j", ")", "{", "SortUtils", ".", "swap", "(", "a", ",", "i", "++", ",", "j", "--", ")", ";", "// code for swap not shown, but easy enough", "}", "}" ]
Reverses the order of the elements in the specified array. @param a an array to reverse.
[ "Reverses", "the", "order", "of", "the", "elements", "in", "the", "specified", "array", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1171-L1176
17,440
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.rowMin
public static double[] rowMin(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = min(data[i]); } return x; }
java
public static double[] rowMin(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = min(data[i]); } return x; }
[ "public", "static", "double", "[", "]", "rowMin", "(", "double", "[", "]", "[", "]", "data", ")", "{", "double", "[", "]", "x", "=", "new", "double", "[", "data", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "=", "min", "(", "data", "[", "i", "]", ")", ";", "}", "return", "x", ";", "}" ]
Returns the row minimum for a matrix.
[ "Returns", "the", "row", "minimum", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1533-L1541
17,441
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.rowMax
public static double[] rowMax(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = max(data[i]); } return x; }
java
public static double[] rowMax(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = max(data[i]); } return x; }
[ "public", "static", "double", "[", "]", "rowMax", "(", "double", "[", "]", "[", "]", "data", ")", "{", "double", "[", "]", "x", "=", "new", "double", "[", "data", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "=", "max", "(", "data", "[", "i", "]", ")", ";", "}", "return", "x", ";", "}" ]
Returns the row maximum for a matrix.
[ "Returns", "the", "row", "maximum", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1546-L1554
17,442
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.rowSums
public static double[] rowSums(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = sum(data[i]); } return x; }
java
public static double[] rowSums(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = sum(data[i]); } return x; }
[ "public", "static", "double", "[", "]", "rowSums", "(", "double", "[", "]", "[", "]", "data", ")", "{", "double", "[", "]", "x", "=", "new", "double", "[", "data", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "=", "sum", "(", "data", "[", "i", "]", ")", ";", "}", "return", "x", ";", "}" ]
Returns the row sums for a matrix.
[ "Returns", "the", "row", "sums", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1559-L1567
17,443
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.rowMeans
public static double[] rowMeans(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = mean(data[i]); } return x; }
java
public static double[] rowMeans(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = mean(data[i]); } return x; }
[ "public", "static", "double", "[", "]", "rowMeans", "(", "double", "[", "]", "[", "]", "data", ")", "{", "double", "[", "]", "x", "=", "new", "double", "[", "data", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "=", "mean", "(", "data", "[", "i", "]", ")", ";", "}", "return", "x", ";", "}" ]
Returns the row means for a matrix.
[ "Returns", "the", "row", "means", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1572-L1580
17,444
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.rowSds
public static double[] rowSds(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = sd(data[i]); } return x; }
java
public static double[] rowSds(double[][] data) { double[] x = new double[data.length]; for (int i = 0; i < x.length; i++) { x[i] = sd(data[i]); } return x; }
[ "public", "static", "double", "[", "]", "rowSds", "(", "double", "[", "]", "[", "]", "data", ")", "{", "double", "[", "]", "x", "=", "new", "double", "[", "data", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "=", "sd", "(", "data", "[", "i", "]", ")", ";", "}", "return", "x", ";", "}" ]
Returns the row standard deviations for a matrix.
[ "Returns", "the", "row", "standard", "deviations", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1585-L1593
17,445
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.colMin
public static double[] colMin(double[][] data) { double[] x = new double[data[0].length]; for (int i = 0; i < x.length; i++) { x[i] = Double.POSITIVE_INFINITY; } for (int i = 0; i < data.length; i++) { for (int j = 0; j < x.length; j++) { if (x[j] > data[i][j]) { x[j] = data[i][j]; } } } return x; }
java
public static double[] colMin(double[][] data) { double[] x = new double[data[0].length]; for (int i = 0; i < x.length; i++) { x[i] = Double.POSITIVE_INFINITY; } for (int i = 0; i < data.length; i++) { for (int j = 0; j < x.length; j++) { if (x[j] > data[i][j]) { x[j] = data[i][j]; } } } return x; }
[ "public", "static", "double", "[", "]", "colMin", "(", "double", "[", "]", "[", "]", "data", ")", "{", "double", "[", "]", "x", "=", "new", "double", "[", "data", "[", "0", "]", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "=", "Double", ".", "POSITIVE_INFINITY", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "data", ".", "length", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "0", ";", "j", "<", "x", ".", "length", ";", "j", "++", ")", "{", "if", "(", "x", "[", "j", "]", ">", "data", "[", "i", "]", "[", "j", "]", ")", "{", "x", "[", "j", "]", "=", "data", "[", "i", "]", "[", "j", "]", ";", "}", "}", "}", "return", "x", ";", "}" ]
Returns the column minimum for a matrix.
[ "Returns", "the", "column", "minimum", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1598-L1613
17,446
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.colMax
public static double[] colMax(double[][] data) { double[] x = new double[data[0].length]; for (int i = 0; i < x.length; i++) { x[i] = Double.NEGATIVE_INFINITY; } for (int i = 0; i < data.length; i++) { for (int j = 0; j < x.length; j++) { if (x[j] < data[i][j]) { x[j] = data[i][j]; } } } return x; }
java
public static double[] colMax(double[][] data) { double[] x = new double[data[0].length]; for (int i = 0; i < x.length; i++) { x[i] = Double.NEGATIVE_INFINITY; } for (int i = 0; i < data.length; i++) { for (int j = 0; j < x.length; j++) { if (x[j] < data[i][j]) { x[j] = data[i][j]; } } } return x; }
[ "public", "static", "double", "[", "]", "colMax", "(", "double", "[", "]", "[", "]", "data", ")", "{", "double", "[", "]", "x", "=", "new", "double", "[", "data", "[", "0", "]", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "=", "Double", ".", "NEGATIVE_INFINITY", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "data", ".", "length", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "0", ";", "j", "<", "x", ".", "length", ";", "j", "++", ")", "{", "if", "(", "x", "[", "j", "]", "<", "data", "[", "i", "]", "[", "j", "]", ")", "{", "x", "[", "j", "]", "=", "data", "[", "i", "]", "[", "j", "]", ";", "}", "}", "}", "return", "x", ";", "}" ]
Returns the column maximum for a matrix.
[ "Returns", "the", "column", "maximum", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1618-L1633
17,447
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.colSums
public static double[] colSums(double[][] data) { double[] x = data[0].clone(); for (int i = 1; i < data.length; i++) { for (int j = 0; j < x.length; j++) { x[j] += data[i][j]; } } return x; }
java
public static double[] colSums(double[][] data) { double[] x = data[0].clone(); for (int i = 1; i < data.length; i++) { for (int j = 0; j < x.length; j++) { x[j] += data[i][j]; } } return x; }
[ "public", "static", "double", "[", "]", "colSums", "(", "double", "[", "]", "[", "]", "data", ")", "{", "double", "[", "]", "x", "=", "data", "[", "0", "]", ".", "clone", "(", ")", ";", "for", "(", "int", "i", "=", "1", ";", "i", "<", "data", ".", "length", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "0", ";", "j", "<", "x", ".", "length", ";", "j", "++", ")", "{", "x", "[", "j", "]", "+=", "data", "[", "i", "]", "[", "j", "]", ";", "}", "}", "return", "x", ";", "}" ]
Returns the column sums for a matrix.
[ "Returns", "the", "column", "sums", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1638-L1648
17,448
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.colMeans
public static double[] colMeans(double[][] data) { double[] x = data[0].clone(); for (int i = 1; i < data.length; i++) { for (int j = 0; j < x.length; j++) { x[j] += data[i][j]; } } scale(1.0 / data.length, x); return x; }
java
public static double[] colMeans(double[][] data) { double[] x = data[0].clone(); for (int i = 1; i < data.length; i++) { for (int j = 0; j < x.length; j++) { x[j] += data[i][j]; } } scale(1.0 / data.length, x); return x; }
[ "public", "static", "double", "[", "]", "colMeans", "(", "double", "[", "]", "[", "]", "data", ")", "{", "double", "[", "]", "x", "=", "data", "[", "0", "]", ".", "clone", "(", ")", ";", "for", "(", "int", "i", "=", "1", ";", "i", "<", "data", ".", "length", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "0", ";", "j", "<", "x", ".", "length", ";", "j", "++", ")", "{", "x", "[", "j", "]", "+=", "data", "[", "i", "]", "[", "j", "]", ";", "}", "}", "scale", "(", "1.0", "/", "data", ".", "length", ",", "x", ")", ";", "return", "x", ";", "}" ]
Returns the column means for a matrix.
[ "Returns", "the", "column", "means", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1653-L1665
17,449
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.colSds
public static double[] colSds(double[][] data) { if (data.length < 2) { throw new IllegalArgumentException("Array length is less than 2."); } int p = data[0].length; double[] sum = new double[p]; double[] sumsq = new double[p]; for (double[] x : data) { for (int i = 0; i < p; i++) { sum[i] += x[i]; sumsq[i] += x[i] * x[i]; } } int n = data.length - 1; for (int i = 0; i < p; i++) { sumsq[i] = java.lang.Math.sqrt(sumsq[i] / n - (sum[i] / data.length) * (sum[i] / n)); } return sumsq; }
java
public static double[] colSds(double[][] data) { if (data.length < 2) { throw new IllegalArgumentException("Array length is less than 2."); } int p = data[0].length; double[] sum = new double[p]; double[] sumsq = new double[p]; for (double[] x : data) { for (int i = 0; i < p; i++) { sum[i] += x[i]; sumsq[i] += x[i] * x[i]; } } int n = data.length - 1; for (int i = 0; i < p; i++) { sumsq[i] = java.lang.Math.sqrt(sumsq[i] / n - (sum[i] / data.length) * (sum[i] / n)); } return sumsq; }
[ "public", "static", "double", "[", "]", "colSds", "(", "double", "[", "]", "[", "]", "data", ")", "{", "if", "(", "data", ".", "length", "<", "2", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Array length is less than 2.\"", ")", ";", "}", "int", "p", "=", "data", "[", "0", "]", ".", "length", ";", "double", "[", "]", "sum", "=", "new", "double", "[", "p", "]", ";", "double", "[", "]", "sumsq", "=", "new", "double", "[", "p", "]", ";", "for", "(", "double", "[", "]", "x", ":", "data", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "p", ";", "i", "++", ")", "{", "sum", "[", "i", "]", "+=", "x", "[", "i", "]", ";", "sumsq", "[", "i", "]", "+=", "x", "[", "i", "]", "*", "x", "[", "i", "]", ";", "}", "}", "int", "n", "=", "data", ".", "length", "-", "1", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "p", ";", "i", "++", ")", "{", "sumsq", "[", "i", "]", "=", "java", ".", "lang", ".", "Math", ".", "sqrt", "(", "sumsq", "[", "i", "]", "/", "n", "-", "(", "sum", "[", "i", "]", "/", "data", ".", "length", ")", "*", "(", "sum", "[", "i", "]", "/", "n", ")", ")", ";", "}", "return", "sumsq", ";", "}" ]
Returns the column deviations for a matrix.
[ "Returns", "the", "column", "deviations", "for", "a", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1670-L1691
17,450
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.sum
public static int sum(int[] x) { double sum = 0.0; for (int n : x) { sum += n; } if (sum > Integer.MAX_VALUE || sum < -Integer.MAX_VALUE) { throw new ArithmeticException("Sum overflow: " + sum); } return (int) sum; }
java
public static int sum(int[] x) { double sum = 0.0; for (int n : x) { sum += n; } if (sum > Integer.MAX_VALUE || sum < -Integer.MAX_VALUE) { throw new ArithmeticException("Sum overflow: " + sum); } return (int) sum; }
[ "public", "static", "int", "sum", "(", "int", "[", "]", "x", ")", "{", "double", "sum", "=", "0.0", ";", "for", "(", "int", "n", ":", "x", ")", "{", "sum", "+=", "n", ";", "}", "if", "(", "sum", ">", "Integer", ".", "MAX_VALUE", "||", "sum", "<", "-", "Integer", ".", "MAX_VALUE", ")", "{", "throw", "new", "ArithmeticException", "(", "\"Sum overflow: \"", "+", "sum", ")", ";", "}", "return", "(", "int", ")", "sum", ";", "}" ]
Returns the sum of an array.
[ "Returns", "the", "sum", "of", "an", "array", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1696-L1708
17,451
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.median
public static <T extends Comparable<? super T>> T median(T[] a) { return QuickSelect.median(a); }
java
public static <T extends Comparable<? super T>> T median(T[] a) { return QuickSelect.median(a); }
[ "public", "static", "<", "T", "extends", "Comparable", "<", "?", "super", "T", ">", ">", "T", "median", "(", "T", "[", "]", "a", ")", "{", "return", "QuickSelect", ".", "median", "(", "a", ")", ";", "}" ]
Find the median of an array of type double. The input array will be rearranged.
[ "Find", "the", "median", "of", "an", "array", "of", "type", "double", ".", "The", "input", "array", "will", "be", "rearranged", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1764-L1766
17,452
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.var
public static double var(int[] x) { if (x.length < 2) { throw new IllegalArgumentException("Array length is less than 2."); } double sum = 0.0; double sumsq = 0.0; for (int xi : x) { sum += xi; sumsq += xi * xi; } int n = x.length - 1; return sumsq / n - (sum / x.length) * (sum / n); }
java
public static double var(int[] x) { if (x.length < 2) { throw new IllegalArgumentException("Array length is less than 2."); } double sum = 0.0; double sumsq = 0.0; for (int xi : x) { sum += xi; sumsq += xi * xi; } int n = x.length - 1; return sumsq / n - (sum / x.length) * (sum / n); }
[ "public", "static", "double", "var", "(", "int", "[", "]", "x", ")", "{", "if", "(", "x", ".", "length", "<", "2", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Array length is less than 2.\"", ")", ";", "}", "double", "sum", "=", "0.0", ";", "double", "sumsq", "=", "0.0", ";", "for", "(", "int", "xi", ":", "x", ")", "{", "sum", "+=", "xi", ";", "sumsq", "+=", "xi", "*", "xi", ";", "}", "int", "n", "=", "x", ".", "length", "-", "1", ";", "return", "sumsq", "/", "n", "-", "(", "sum", "/", "x", ".", "length", ")", "*", "(", "sum", "/", "n", ")", ";", "}" ]
Returns the variance of an array.
[ "Returns", "the", "variance", "of", "an", "array", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L1856-L1870
17,453
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.squaredDistance
public static double squaredDistance(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Input vector sizes are different."); } double sum = 0.0; for (int i = 0; i < x.length; i++) { sum += sqr(x[i] - y[i]); } return sum; }
java
public static double squaredDistance(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Input vector sizes are different."); } double sum = 0.0; for (int i = 0; i < x.length; i++) { sum += sqr(x[i] - y[i]); } return sum; }
[ "public", "static", "double", "squaredDistance", "(", "int", "[", "]", "x", ",", "int", "[", "]", "y", ")", "{", "if", "(", "x", ".", "length", "!=", "y", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Input vector sizes are different.\"", ")", ";", "}", "double", "sum", "=", "0.0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "sum", "+=", "sqr", "(", "x", "[", "i", "]", "-", "y", "[", "i", "]", ")", ";", "}", "return", "sum", ";", "}" ]
The squared Euclidean distance.
[ "The", "squared", "Euclidean", "distance", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L2160-L2171
17,454
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.dot
public static double dot(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Arrays have different length."); } double p = 0.0; for (int i = 0; i < x.length; i++) { p += x[i] * y[i]; } return p; }
java
public static double dot(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Arrays have different length."); } double p = 0.0; for (int i = 0; i < x.length; i++) { p += x[i] * y[i]; } return p; }
[ "public", "static", "double", "dot", "(", "int", "[", "]", "x", ",", "int", "[", "]", "y", ")", "{", "if", "(", "x", ".", "length", "!=", "y", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Arrays have different length.\"", ")", ";", "}", "double", "p", "=", "0.0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "p", "+=", "x", "[", "i", "]", "*", "y", "[", "i", "]", ";", "}", "return", "p", ";", "}" ]
Returns the dot product between two vectors.
[ "Returns", "the", "dot", "product", "between", "two", "vectors", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L2497-L2508
17,455
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.dot
public static double dot(SparseArray x, SparseArray y) { Iterator<SparseArray.Entry> it1 = x.iterator(); Iterator<SparseArray.Entry> it2 = y.iterator(); SparseArray.Entry e1 = it1.hasNext() ? it1.next() : null; SparseArray.Entry e2 = it2.hasNext() ? it2.next() : null; double s = 0.0; while (e1 != null && e2 != null) { if (e1.i == e2.i) { s += e1.x * e2.x; e1 = it1.hasNext() ? it1.next() : null; e2 = it2.hasNext() ? it2.next() : null; } else if (e1.i > e2.i) { e2 = it2.hasNext() ? it2.next() : null; } else { e1 = it1.hasNext() ? it1.next() : null; } } return s; }
java
public static double dot(SparseArray x, SparseArray y) { Iterator<SparseArray.Entry> it1 = x.iterator(); Iterator<SparseArray.Entry> it2 = y.iterator(); SparseArray.Entry e1 = it1.hasNext() ? it1.next() : null; SparseArray.Entry e2 = it2.hasNext() ? it2.next() : null; double s = 0.0; while (e1 != null && e2 != null) { if (e1.i == e2.i) { s += e1.x * e2.x; e1 = it1.hasNext() ? it1.next() : null; e2 = it2.hasNext() ? it2.next() : null; } else if (e1.i > e2.i) { e2 = it2.hasNext() ? it2.next() : null; } else { e1 = it1.hasNext() ? it1.next() : null; } } return s; }
[ "public", "static", "double", "dot", "(", "SparseArray", "x", ",", "SparseArray", "y", ")", "{", "Iterator", "<", "SparseArray", ".", "Entry", ">", "it1", "=", "x", ".", "iterator", "(", ")", ";", "Iterator", "<", "SparseArray", ".", "Entry", ">", "it2", "=", "y", ".", "iterator", "(", ")", ";", "SparseArray", ".", "Entry", "e1", "=", "it1", ".", "hasNext", "(", ")", "?", "it1", ".", "next", "(", ")", ":", "null", ";", "SparseArray", ".", "Entry", "e2", "=", "it2", ".", "hasNext", "(", ")", "?", "it2", ".", "next", "(", ")", ":", "null", ";", "double", "s", "=", "0.0", ";", "while", "(", "e1", "!=", "null", "&&", "e2", "!=", "null", ")", "{", "if", "(", "e1", ".", "i", "==", "e2", ".", "i", ")", "{", "s", "+=", "e1", ".", "x", "*", "e2", ".", "x", ";", "e1", "=", "it1", ".", "hasNext", "(", ")", "?", "it1", ".", "next", "(", ")", ":", "null", ";", "e2", "=", "it2", ".", "hasNext", "(", ")", "?", "it2", ".", "next", "(", ")", ":", "null", ";", "}", "else", "if", "(", "e1", ".", "i", ">", "e2", ".", "i", ")", "{", "e2", "=", "it2", ".", "hasNext", "(", ")", "?", "it2", ".", "next", "(", ")", ":", "null", ";", "}", "else", "{", "e1", "=", "it1", ".", "hasNext", "(", ")", "?", "it1", ".", "next", "(", ")", ":", "null", ";", "}", "}", "return", "s", ";", "}" ]
Returns the dot product between two sparse arrays.
[ "Returns", "the", "dot", "product", "between", "two", "sparse", "arrays", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L2545-L2565
17,456
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.cov
public static double cov(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Arrays have different length."); } if (x.length < 3) { throw new IllegalArgumentException("array length has to be at least 3."); } double mx = mean(x); double my = mean(y); double Sxy = 0.0; for (int i = 0; i < x.length; i++) { double dx = x[i] - mx; double dy = y[i] - my; Sxy += dx * dy; } return Sxy / (x.length - 1); }
java
public static double cov(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Arrays have different length."); } if (x.length < 3) { throw new IllegalArgumentException("array length has to be at least 3."); } double mx = mean(x); double my = mean(y); double Sxy = 0.0; for (int i = 0; i < x.length; i++) { double dx = x[i] - mx; double dy = y[i] - my; Sxy += dx * dy; } return Sxy / (x.length - 1); }
[ "public", "static", "double", "cov", "(", "int", "[", "]", "x", ",", "int", "[", "]", "y", ")", "{", "if", "(", "x", ".", "length", "!=", "y", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Arrays have different length.\"", ")", ";", "}", "if", "(", "x", ".", "length", "<", "3", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"array length has to be at least 3.\"", ")", ";", "}", "double", "mx", "=", "mean", "(", "x", ")", ";", "double", "my", "=", "mean", "(", "y", ")", ";", "double", "Sxy", "=", "0.0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "double", "dx", "=", "x", "[", "i", "]", "-", "mx", ";", "double", "dy", "=", "y", "[", "i", "]", "-", "my", ";", "Sxy", "+=", "dx", "*", "dy", ";", "}", "return", "Sxy", "/", "(", "x", ".", "length", "-", "1", ")", ";", "}" ]
Returns the covariance between two vectors.
[ "Returns", "the", "covariance", "between", "two", "vectors", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L2570-L2590
17,457
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.cov
public static double[][] cov(double[][] data, double[] mu) { double[][] sigma = new double[data[0].length][data[0].length]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < mu.length; j++) { for (int k = 0; k <= j; k++) { sigma[j][k] += (data[i][j] - mu[j]) * (data[i][k] - mu[k]); } } } int n = data.length - 1; for (int j = 0; j < mu.length; j++) { for (int k = 0; k <= j; k++) { sigma[j][k] /= n; sigma[k][j] = sigma[j][k]; } } return sigma; }
java
public static double[][] cov(double[][] data, double[] mu) { double[][] sigma = new double[data[0].length][data[0].length]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < mu.length; j++) { for (int k = 0; k <= j; k++) { sigma[j][k] += (data[i][j] - mu[j]) * (data[i][k] - mu[k]); } } } int n = data.length - 1; for (int j = 0; j < mu.length; j++) { for (int k = 0; k <= j; k++) { sigma[j][k] /= n; sigma[k][j] = sigma[j][k]; } } return sigma; }
[ "public", "static", "double", "[", "]", "[", "]", "cov", "(", "double", "[", "]", "[", "]", "data", ",", "double", "[", "]", "mu", ")", "{", "double", "[", "]", "[", "]", "sigma", "=", "new", "double", "[", "data", "[", "0", "]", ".", "length", "]", "[", "data", "[", "0", "]", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "data", ".", "length", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "0", ";", "j", "<", "mu", ".", "length", ";", "j", "++", ")", "{", "for", "(", "int", "k", "=", "0", ";", "k", "<=", "j", ";", "k", "++", ")", "{", "sigma", "[", "j", "]", "[", "k", "]", "+=", "(", "data", "[", "i", "]", "[", "j", "]", "-", "mu", "[", "j", "]", ")", "*", "(", "data", "[", "i", "]", "[", "k", "]", "-", "mu", "[", "k", "]", ")", ";", "}", "}", "}", "int", "n", "=", "data", ".", "length", "-", "1", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "mu", ".", "length", ";", "j", "++", ")", "{", "for", "(", "int", "k", "=", "0", ";", "k", "<=", "j", ";", "k", "++", ")", "{", "sigma", "[", "j", "]", "[", "k", "]", "/=", "n", ";", "sigma", "[", "k", "]", "[", "j", "]", "=", "sigma", "[", "j", "]", "[", "k", "]", ";", "}", "}", "return", "sigma", ";", "}" ]
Returns the sample covariance matrix. @param mu the known mean of data.
[ "Returns", "the", "sample", "covariance", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L2653-L2672
17,458
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.cor
public static double cor(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Arrays have different length."); } if (x.length < 3) { throw new IllegalArgumentException("array length has to be at least 3."); } double Sxy = cov(x, y); double Sxx = var(x); double Syy = var(y); if (Sxx == 0 || Syy == 0) { return Double.NaN; } return Sxy / java.lang.Math.sqrt(Sxx * Syy); }
java
public static double cor(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Arrays have different length."); } if (x.length < 3) { throw new IllegalArgumentException("array length has to be at least 3."); } double Sxy = cov(x, y); double Sxx = var(x); double Syy = var(y); if (Sxx == 0 || Syy == 0) { return Double.NaN; } return Sxy / java.lang.Math.sqrt(Sxx * Syy); }
[ "public", "static", "double", "cor", "(", "int", "[", "]", "x", ",", "int", "[", "]", "y", ")", "{", "if", "(", "x", ".", "length", "!=", "y", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Arrays have different length.\"", ")", ";", "}", "if", "(", "x", ".", "length", "<", "3", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"array length has to be at least 3.\"", ")", ";", "}", "double", "Sxy", "=", "cov", "(", "x", ",", "y", ")", ";", "double", "Sxx", "=", "var", "(", "x", ")", ";", "double", "Syy", "=", "var", "(", "y", ")", ";", "if", "(", "Sxx", "==", "0", "||", "Syy", "==", "0", ")", "{", "return", "Double", ".", "NaN", ";", "}", "return", "Sxy", "/", "java", ".", "lang", ".", "Math", ".", "sqrt", "(", "Sxx", "*", "Syy", ")", ";", "}" ]
Returns the correlation coefficient between two vectors.
[ "Returns", "the", "correlation", "coefficient", "between", "two", "vectors", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L2677-L2695
17,459
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.cor
public static double[][] cor(double[][] data, double[] mu) { double[][] sigma = cov(data, mu); int n = data[0].length; double[] sd = new double[n]; for (int i = 0; i < n; i++) { sd[i] = sqrt(sigma[i][i]); } for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { sigma[i][j] /= sd[i] * sd[j]; sigma[j][i] = sigma[i][j]; } } return sigma; }
java
public static double[][] cor(double[][] data, double[] mu) { double[][] sigma = cov(data, mu); int n = data[0].length; double[] sd = new double[n]; for (int i = 0; i < n; i++) { sd[i] = sqrt(sigma[i][i]); } for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { sigma[i][j] /= sd[i] * sd[j]; sigma[j][i] = sigma[i][j]; } } return sigma; }
[ "public", "static", "double", "[", "]", "[", "]", "cor", "(", "double", "[", "]", "[", "]", "data", ",", "double", "[", "]", "mu", ")", "{", "double", "[", "]", "[", "]", "sigma", "=", "cov", "(", "data", ",", "mu", ")", ";", "int", "n", "=", "data", "[", "0", "]", ".", "length", ";", "double", "[", "]", "sd", "=", "new", "double", "[", "n", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "sd", "[", "i", "]", "=", "sqrt", "(", "sigma", "[", "i", "]", "[", "i", "]", ")", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "0", ";", "j", "<=", "i", ";", "j", "++", ")", "{", "sigma", "[", "i", "]", "[", "j", "]", "/=", "sd", "[", "i", "]", "*", "sd", "[", "j", "]", ";", "sigma", "[", "j", "]", "[", "i", "]", "=", "sigma", "[", "i", "]", "[", "j", "]", ";", "}", "}", "return", "sigma", ";", "}" ]
Returns the sample correlation matrix. @param mu the known mean of data.
[ "Returns", "the", "sample", "correlation", "matrix", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L2754-L2771
17,460
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.kendall
public static double kendall(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Input vector sizes are different."); } int is = 0, n2 = 0, n1 = 0, n = x.length; double aa, a2, a1; for (int j = 0; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { a1 = x[j] - x[k]; a2 = y[j] - y[k]; aa = a1 * a2; if (aa != 0.0) { ++n1; ++n2; if (aa > 0) { ++is; } else { --is; } } else { if (a1 != 0.0) { ++n1; } if (a2 != 0.0) { ++n2; } } } } double tau = is / (Math.sqrt(n1) * Math.sqrt(n2)); return tau; }
java
public static double kendall(int[] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException("Input vector sizes are different."); } int is = 0, n2 = 0, n1 = 0, n = x.length; double aa, a2, a1; for (int j = 0; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { a1 = x[j] - x[k]; a2 = y[j] - y[k]; aa = a1 * a2; if (aa != 0.0) { ++n1; ++n2; if (aa > 0) { ++is; } else { --is; } } else { if (a1 != 0.0) { ++n1; } if (a2 != 0.0) { ++n2; } } } } double tau = is / (Math.sqrt(n1) * Math.sqrt(n2)); return tau; }
[ "public", "static", "double", "kendall", "(", "int", "[", "]", "x", ",", "int", "[", "]", "y", ")", "{", "if", "(", "x", ".", "length", "!=", "y", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Input vector sizes are different.\"", ")", ";", "}", "int", "is", "=", "0", ",", "n2", "=", "0", ",", "n1", "=", "0", ",", "n", "=", "x", ".", "length", ";", "double", "aa", ",", "a2", ",", "a1", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "n", "-", "1", ";", "j", "++", ")", "{", "for", "(", "int", "k", "=", "j", "+", "1", ";", "k", "<", "n", ";", "k", "++", ")", "{", "a1", "=", "x", "[", "j", "]", "-", "x", "[", "k", "]", ";", "a2", "=", "y", "[", "j", "]", "-", "y", "[", "k", "]", ";", "aa", "=", "a1", "*", "a2", ";", "if", "(", "aa", "!=", "0.0", ")", "{", "++", "n1", ";", "++", "n2", ";", "if", "(", "aa", ">", "0", ")", "{", "++", "is", ";", "}", "else", "{", "--", "is", ";", "}", "}", "else", "{", "if", "(", "a1", "!=", "0.0", ")", "{", "++", "n1", ";", "}", "if", "(", "a2", "!=", "0.0", ")", "{", "++", "n2", ";", "}", "}", "}", "}", "double", "tau", "=", "is", "/", "(", "Math", ".", "sqrt", "(", "n1", ")", "*", "Math", ".", "sqrt", "(", "n2", ")", ")", ";", "return", "tau", ";", "}" ]
The Kendall Tau Rank Correlation Coefficient is used to measure the degree of correspondence between sets of rankings where the measures are not equidistant. It is used with non-parametric data.
[ "The", "Kendall", "Tau", "Rank", "Correlation", "Coefficient", "is", "used", "to", "measure", "the", "degree", "of", "correspondence", "between", "sets", "of", "rankings", "where", "the", "measures", "are", "not", "equidistant", ".", "It", "is", "used", "with", "non", "-", "parametric", "data", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L2923-L2957
17,461
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.norm1
public static double norm1(double[] x) { double norm = 0.0; for (double n : x) { norm += Math.abs(n); } return norm; }
java
public static double norm1(double[] x) { double norm = 0.0; for (double n : x) { norm += Math.abs(n); } return norm; }
[ "public", "static", "double", "norm1", "(", "double", "[", "]", "x", ")", "{", "double", "norm", "=", "0.0", ";", "for", "(", "double", "n", ":", "x", ")", "{", "norm", "+=", "Math", ".", "abs", "(", "n", ")", ";", "}", "return", "norm", ";", "}" ]
L1 vector norm.
[ "L1", "vector", "norm", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3044-L3052
17,462
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.norm2
public static double norm2(double[] x) { double norm = 0.0; for (double n : x) { norm += n * n; } norm = Math.sqrt(norm); return norm; }
java
public static double norm2(double[] x) { double norm = 0.0; for (double n : x) { norm += n * n; } norm = Math.sqrt(norm); return norm; }
[ "public", "static", "double", "norm2", "(", "double", "[", "]", "x", ")", "{", "double", "norm", "=", "0.0", ";", "for", "(", "double", "n", ":", "x", ")", "{", "norm", "+=", "n", "*", "n", ";", "}", "norm", "=", "Math", ".", "sqrt", "(", "norm", ")", ";", "return", "norm", ";", "}" ]
L2 vector norm.
[ "L2", "vector", "norm", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3057-L3067
17,463
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.normInf
public static double normInf(double[] x) { int n = x.length; double f = Math.abs(x[0]); for (int i = 1; i < n; i++) { f = Math.max(f, Math.abs(x[i])); } return f; }
java
public static double normInf(double[] x) { int n = x.length; double f = Math.abs(x[0]); for (int i = 1; i < n; i++) { f = Math.max(f, Math.abs(x[i])); } return f; }
[ "public", "static", "double", "normInf", "(", "double", "[", "]", "x", ")", "{", "int", "n", "=", "x", ".", "length", ";", "double", "f", "=", "Math", ".", "abs", "(", "x", "[", "0", "]", ")", ";", "for", "(", "int", "i", "=", "1", ";", "i", "<", "n", ";", "i", "++", ")", "{", "f", "=", "Math", ".", "max", "(", "f", ",", "Math", ".", "abs", "(", "x", "[", "i", "]", ")", ")", ";", "}", "return", "f", ";", "}" ]
L-infinity vector norm. Maximum absolute value.
[ "L", "-", "infinity", "vector", "norm", ".", "Maximum", "absolute", "value", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3072-L3081
17,464
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.standardize
public static void standardize(double[] x) { double mu = mean(x); double sigma = sd(x); if (isZero(sigma)) { logger.warn("array has variance of 0."); return; } for (int i = 0; i < x.length; i++) { x[i] = (x[i] - mu) / sigma; } }
java
public static void standardize(double[] x) { double mu = mean(x); double sigma = sd(x); if (isZero(sigma)) { logger.warn("array has variance of 0."); return; } for (int i = 0; i < x.length; i++) { x[i] = (x[i] - mu) / sigma; } }
[ "public", "static", "void", "standardize", "(", "double", "[", "]", "x", ")", "{", "double", "mu", "=", "mean", "(", "x", ")", ";", "double", "sigma", "=", "sd", "(", "x", ")", ";", "if", "(", "isZero", "(", "sigma", ")", ")", "{", "logger", ".", "warn", "(", "\"array has variance of 0.\"", ")", ";", "return", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "=", "(", "x", "[", "i", "]", "-", "mu", ")", "/", "sigma", ";", "}", "}" ]
Standardizes an array to mean 0 and variance 1.
[ "Standardizes", "an", "array", "to", "mean", "0", "and", "variance", "1", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3093-L3105
17,465
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.standardize
public static void standardize(double[][] x) { int n = x.length; int p = x[0].length; double[] center = colMeans(x); for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { x[i][j] = x[i][j] - center[j]; } } double[] scale = new double[p]; for (int j = 0; j < p; j++) { for (int i = 0; i < n; i++) { scale[j] += Math.sqr(x[i][j]); } scale[j] = Math.sqrt(scale[j] / (n-1)); if (!Math.isZero(scale[j])) { for (int i = 0; i < n; i++) { x[i][j] /= scale[j]; } } } }
java
public static void standardize(double[][] x) { int n = x.length; int p = x[0].length; double[] center = colMeans(x); for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { x[i][j] = x[i][j] - center[j]; } } double[] scale = new double[p]; for (int j = 0; j < p; j++) { for (int i = 0; i < n; i++) { scale[j] += Math.sqr(x[i][j]); } scale[j] = Math.sqrt(scale[j] / (n-1)); if (!Math.isZero(scale[j])) { for (int i = 0; i < n; i++) { x[i][j] /= scale[j]; } } } }
[ "public", "static", "void", "standardize", "(", "double", "[", "]", "[", "]", "x", ")", "{", "int", "n", "=", "x", ".", "length", ";", "int", "p", "=", "x", "[", "0", "]", ".", "length", ";", "double", "[", "]", "center", "=", "colMeans", "(", "x", ")", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "0", ";", "j", "<", "p", ";", "j", "++", ")", "{", "x", "[", "i", "]", "[", "j", "]", "=", "x", "[", "i", "]", "[", "j", "]", "-", "center", "[", "j", "]", ";", "}", "}", "double", "[", "]", "scale", "=", "new", "double", "[", "p", "]", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "p", ";", "j", "++", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "scale", "[", "j", "]", "+=", "Math", ".", "sqr", "(", "x", "[", "i", "]", "[", "j", "]", ")", ";", "}", "scale", "[", "j", "]", "=", "Math", ".", "sqrt", "(", "scale", "[", "j", "]", "/", "(", "n", "-", "1", ")", ")", ";", "if", "(", "!", "Math", ".", "isZero", "(", "scale", "[", "j", "]", ")", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "x", "[", "i", "]", "[", "j", "]", "/=", "scale", "[", "j", "]", ";", "}", "}", "}", "}" ]
Standardizes each column of a matrix to 0 mean and unit variance.
[ "Standardizes", "each", "column", "of", "a", "matrix", "to", "0", "mean", "and", "unit", "variance", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3143-L3167
17,466
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.unitize1
public static void unitize1(double[] x) { double n = norm1(x); for (int i = 0; i < x.length; i++) { x[i] /= n; } }
java
public static void unitize1(double[] x) { double n = norm1(x); for (int i = 0; i < x.length; i++) { x[i] /= n; } }
[ "public", "static", "void", "unitize1", "(", "double", "[", "]", "x", ")", "{", "double", "n", "=", "norm1", "(", "x", ")", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "/=", "n", ";", "}", "}" ]
Unitize an array so that L1 norm of x is 1. @param x an array of nonnegative double
[ "Unitize", "an", "array", "so", "that", "L1", "norm", "of", "x", "is", "1", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3224-L3230
17,467
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.unitize2
public static void unitize2(double[] x) { double n = norm(x); for (int i = 0; i < x.length; i++) { x[i] /= n; } }
java
public static void unitize2(double[] x) { double n = norm(x); for (int i = 0; i < x.length; i++) { x[i] /= n; } }
[ "public", "static", "void", "unitize2", "(", "double", "[", "]", "x", ")", "{", "double", "n", "=", "norm", "(", "x", ")", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "x", "[", "i", "]", "/=", "n", ";", "}", "}" ]
Unitize an array so that L2 norm of x = 1. @param x the array of double
[ "Unitize", "an", "array", "so", "that", "L2", "norm", "of", "x", "=", "1", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3237-L3243
17,468
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.row
private static int row(int[] r, int f) { int i = 0; while (i < r.length && r[i] < f) { ++i; } return ((i < r.length && r[i] == f) ? i : -1); }
java
private static int row(int[] r, int f) { int i = 0; while (i < r.length && r[i] < f) { ++i; } return ((i < r.length && r[i] == f) ? i : -1); }
[ "private", "static", "int", "row", "(", "int", "[", "]", "r", ",", "int", "f", ")", "{", "int", "i", "=", "0", ";", "while", "(", "i", "<", "r", ".", "length", "&&", "r", "[", "i", "]", "<", "f", ")", "{", "++", "i", ";", "}", "return", "(", "(", "i", "<", "r", ".", "length", "&&", "r", "[", "i", "]", "==", "f", ")", "?", "i", ":", "-", "1", ")", ";", "}" ]
Returns the index of given frequency. @param r the frequency list. @param f the given frequency. @return the index of given frequency or -1 if it doesn't exist in the list.
[ "Returns", "the", "index", "of", "given", "frequency", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3258-L3266
17,469
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.swap
public static void swap(int[] x, int i, int j) { int s = x[i]; x[i] = x[j]; x[j] = s; }
java
public static void swap(int[] x, int i, int j) { int s = x[i]; x[i] = x[j]; x[j] = s; }
[ "public", "static", "void", "swap", "(", "int", "[", "]", "x", ",", "int", "i", ",", "int", "j", ")", "{", "int", "s", "=", "x", "[", "i", "]", ";", "x", "[", "i", "]", "=", "x", "[", "j", "]", ";", "x", "[", "j", "]", "=", "s", ";", "}" ]
Swap two elements of an array.
[ "Swap", "two", "elements", "of", "an", "array", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3592-L3596
17,470
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.swap
public static <E> void swap(E[] x, E[] y) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); } for (int i = 0; i < x.length; i++) { E s = x[i]; x[i] = y[i]; y[i] = s; } }
java
public static <E> void swap(E[] x, E[] y) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); } for (int i = 0; i < x.length; i++) { E s = x[i]; x[i] = y[i]; y[i] = s; } }
[ "public", "static", "<", "E", ">", "void", "swap", "(", "E", "[", "]", "x", ",", "E", "[", "]", "y", ")", "{", "if", "(", "x", ".", "length", "!=", "y", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"Arrays have different length: x[%d], y[%d]\"", ",", "x", ".", "length", ",", "y", ".", "length", ")", ")", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "E", "s", "=", "x", "[", "i", "]", ";", "x", "[", "i", "]", "=", "y", "[", "i", "]", ";", "y", "[", "i", "]", "=", "s", ";", "}", "}" ]
Swap two arrays.
[ "Swap", "two", "arrays", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3673-L3683
17,471
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.plus
public static void plus(double[] y, double[] x) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); } for (int i = 0; i < x.length; i++) { y[i] += x[i]; } }
java
public static void plus(double[] y, double[] x) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); } for (int i = 0; i < x.length; i++) { y[i] += x[i]; } }
[ "public", "static", "void", "plus", "(", "double", "[", "]", "y", ",", "double", "[", "]", "x", ")", "{", "if", "(", "x", ".", "length", "!=", "y", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"Arrays have different length: x[%d], y[%d]\"", ",", "x", ".", "length", ",", "y", ".", "length", ")", ")", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "y", "[", "i", "]", "+=", "x", "[", "i", "]", ";", "}", "}" ]
Element-wise sum of two arrays y = x + y.
[ "Element", "-", "wise", "sum", "of", "two", "arrays", "y", "=", "x", "+", "y", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3760-L3768
17,472
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.minus
public static void minus(double[] y, double[] x) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); } for (int i = 0; i < x.length; i++) { y[i] -= x[i]; } }
java
public static void minus(double[] y, double[] x) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); } for (int i = 0; i < x.length; i++) { y[i] -= x[i]; } }
[ "public", "static", "void", "minus", "(", "double", "[", "]", "y", ",", "double", "[", "]", "x", ")", "{", "if", "(", "x", ".", "length", "!=", "y", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"Arrays have different length: x[%d], y[%d]\"", ",", "x", ".", "length", ",", "y", ".", "length", ")", ")", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "y", "[", "i", "]", "-=", "x", "[", "i", "]", ";", "}", "}" ]
Element-wise subtraction of two arrays y = y - x. @param y minuend matrix @param x subtrahend matrix
[ "Element", "-", "wise", "subtraction", "of", "two", "arrays", "y", "=", "y", "-", "x", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3775-L3783
17,473
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.pow
public static double[] pow(double[] x, double n) { double[] array = new double[x.length]; for (int i = 0; i < x.length; i++) { array[i] = Math.pow(x[i], n); } return array; }
java
public static double[] pow(double[] x, double n) { double[] array = new double[x.length]; for (int i = 0; i < x.length; i++) { array[i] = Math.pow(x[i], n); } return array; }
[ "public", "static", "double", "[", "]", "pow", "(", "double", "[", "]", "x", ",", "double", "n", ")", "{", "double", "[", "]", "array", "=", "new", "double", "[", "x", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "array", "[", "i", "]", "=", "Math", ".", "pow", "(", "x", "[", "i", "]", ",", "n", ")", ";", "}", "return", "array", ";", "}" ]
Raise each element of an array to a scalar power. @param x array @param n scalar exponent @return x<sup>n</sup>
[ "Raise", "each", "element", "of", "an", "array", "to", "a", "scalar", "power", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3824-L3830
17,474
haifengl/smile
math/src/main/java/smile/math/Math.java
Math.sort
public static int[][] sort(double[][] x) { int n = x.length; int p = x[0].length; double[] a = new double[n]; int[][] index = new int[p][]; for (int j = 0; j < p; j++) { for (int i = 0; i < n; i++) { a[i] = x[i][j]; } index[j] = QuickSort.sort(a); } return index; }
java
public static int[][] sort(double[][] x) { int n = x.length; int p = x[0].length; double[] a = new double[n]; int[][] index = new int[p][]; for (int j = 0; j < p; j++) { for (int i = 0; i < n; i++) { a[i] = x[i][j]; } index[j] = QuickSort.sort(a); } return index; }
[ "public", "static", "int", "[", "]", "[", "]", "sort", "(", "double", "[", "]", "[", "]", "x", ")", "{", "int", "n", "=", "x", ".", "length", ";", "int", "p", "=", "x", "[", "0", "]", ".", "length", ";", "double", "[", "]", "a", "=", "new", "double", "[", "n", "]", ";", "int", "[", "]", "[", "]", "index", "=", "new", "int", "[", "p", "]", "[", "", "]", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "p", ";", "j", "++", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "a", "[", "i", "]", "=", "x", "[", "i", "]", "[", "j", "]", ";", "}", "index", "[", "j", "]", "=", "QuickSort", ".", "sort", "(", "a", ")", ";", "}", "return", "index", ";", "}" ]
Sorts each variable and returns the index of values in ascending order. Note that the order of original array is NOT altered. @param x a set of variables to be sorted. Each row is an instance. Each column is a variable. @return the index of values in ascending order
[ "Sorts", "each", "variable", "and", "returns", "the", "index", "of", "values", "in", "ascending", "order", ".", "Note", "that", "the", "order", "of", "original", "array", "is", "NOT", "altered", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Math.java#L3880-L3895
17,475
haifengl/smile
symbolic/src/main/java/smile/symbolic/ExpressionTree.java
ExpressionTree.createInfix
public String createInfix(Expression root) { String str = ""; String closeParen = ""; String leftOpenParen = ""; String leftCloseParen = ""; if(root == null) { return str; } if (ExpressionParser.isOperand(root.getType(), var)) { str += root.getType(); } else if(root.getType().equals("$")) { str += "-"; } else if(ExpressionParser.isFunction(root.getType())) { // does not include unary minus str += root.getType(); str += "("; closeParen = ")"; } else { // is operator int parentPrecedence = ExpressionParser.getPrecedence(root.getType()); str += root.getType(); if(ExpressionParser.isOperator(root.getLeftChild().getType()) && (ExpressionParser.getPrecedence(root.getLeftChild().getType()) < parentPrecedence)) { leftOpenParen = "("; leftCloseParen = ")"; } else if(ExpressionParser.isOperator(root.getRightChild().getType()) && (ExpressionParser.getPrecedence(root.getRightChild().getType()) < parentPrecedence)) { str += "("; closeParen = ")"; } } return leftOpenParen + createInfix(root.getLeftChild()) + leftCloseParen + str + createInfix(root.getRightChild()) + closeParen; }
java
public String createInfix(Expression root) { String str = ""; String closeParen = ""; String leftOpenParen = ""; String leftCloseParen = ""; if(root == null) { return str; } if (ExpressionParser.isOperand(root.getType(), var)) { str += root.getType(); } else if(root.getType().equals("$")) { str += "-"; } else if(ExpressionParser.isFunction(root.getType())) { // does not include unary minus str += root.getType(); str += "("; closeParen = ")"; } else { // is operator int parentPrecedence = ExpressionParser.getPrecedence(root.getType()); str += root.getType(); if(ExpressionParser.isOperator(root.getLeftChild().getType()) && (ExpressionParser.getPrecedence(root.getLeftChild().getType()) < parentPrecedence)) { leftOpenParen = "("; leftCloseParen = ")"; } else if(ExpressionParser.isOperator(root.getRightChild().getType()) && (ExpressionParser.getPrecedence(root.getRightChild().getType()) < parentPrecedence)) { str += "("; closeParen = ")"; } } return leftOpenParen + createInfix(root.getLeftChild()) + leftCloseParen + str + createInfix(root.getRightChild()) + closeParen; }
[ "public", "String", "createInfix", "(", "Expression", "root", ")", "{", "String", "str", "=", "\"\"", ";", "String", "closeParen", "=", "\"\"", ";", "String", "leftOpenParen", "=", "\"\"", ";", "String", "leftCloseParen", "=", "\"\"", ";", "if", "(", "root", "==", "null", ")", "{", "return", "str", ";", "}", "if", "(", "ExpressionParser", ".", "isOperand", "(", "root", ".", "getType", "(", ")", ",", "var", ")", ")", "{", "str", "+=", "root", ".", "getType", "(", ")", ";", "}", "else", "if", "(", "root", ".", "getType", "(", ")", ".", "equals", "(", "\"$\"", ")", ")", "{", "str", "+=", "\"-\"", ";", "}", "else", "if", "(", "ExpressionParser", ".", "isFunction", "(", "root", ".", "getType", "(", ")", ")", ")", "{", "// does not include unary minus", "str", "+=", "root", ".", "getType", "(", ")", ";", "str", "+=", "\"(\"", ";", "closeParen", "=", "\")\"", ";", "}", "else", "{", "// is operator", "int", "parentPrecedence", "=", "ExpressionParser", ".", "getPrecedence", "(", "root", ".", "getType", "(", ")", ")", ";", "str", "+=", "root", ".", "getType", "(", ")", ";", "if", "(", "ExpressionParser", ".", "isOperator", "(", "root", ".", "getLeftChild", "(", ")", ".", "getType", "(", ")", ")", "&&", "(", "ExpressionParser", ".", "getPrecedence", "(", "root", ".", "getLeftChild", "(", ")", ".", "getType", "(", ")", ")", "<", "parentPrecedence", ")", ")", "{", "leftOpenParen", "=", "\"(\"", ";", "leftCloseParen", "=", "\")\"", ";", "}", "else", "if", "(", "ExpressionParser", ".", "isOperator", "(", "root", ".", "getRightChild", "(", ")", ".", "getType", "(", ")", ")", "&&", "(", "ExpressionParser", ".", "getPrecedence", "(", "root", ".", "getRightChild", "(", ")", ".", "getType", "(", ")", ")", "<", "parentPrecedence", ")", ")", "{", "str", "+=", "\"(\"", ";", "closeParen", "=", "\")\"", ";", "}", "}", "return", "leftOpenParen", "+", "createInfix", "(", "root", ".", "getLeftChild", "(", ")", ")", "+", "leftCloseParen", "+", "str", "+", "createInfix", "(", "root", ".", "getRightChild", "(", ")", ")", "+", "closeParen", ";", "}" ]
creates string representing infix expression
[ "creates", "string", "representing", "infix", "expression" ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/symbolic/src/main/java/smile/symbolic/ExpressionTree.java#L121-L160
17,476
haifengl/smile
symbolic/src/main/java/smile/symbolic/ExpressionTree.java
ExpressionTree.constructTree
public Expression constructTree(ArrayList<String> postTokens) { Expression root = null; Stack<Expression> nodes = new Stack<>(); for(String str: postTokens) { if(str.isEmpty()){ continue; } if(str.matches("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")) { nodes.push(new Constant(Double.parseDouble(str))); } else if(str.equals(var)) { nodes.push(new Variable(var)); } else if(!nodes.isEmpty() && ExpressionParser.isFunction(str)) { Expression function = matchFunc(str, nodes.pop()); nodes.push(function); } else if(!nodes.isEmpty() && str.equals("$")) { Expression unaryMinus = new Negation(nodes.pop()); nodes.push(unaryMinus); } else if(!nodes.isEmpty()){ Expression right = nodes.pop(); Expression binaryOperator = matchOperator(str, nodes.pop(), right); nodes.push(binaryOperator); } } if(!nodes.isEmpty()) root = nodes.pop(); return root; }
java
public Expression constructTree(ArrayList<String> postTokens) { Expression root = null; Stack<Expression> nodes = new Stack<>(); for(String str: postTokens) { if(str.isEmpty()){ continue; } if(str.matches("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")) { nodes.push(new Constant(Double.parseDouble(str))); } else if(str.equals(var)) { nodes.push(new Variable(var)); } else if(!nodes.isEmpty() && ExpressionParser.isFunction(str)) { Expression function = matchFunc(str, nodes.pop()); nodes.push(function); } else if(!nodes.isEmpty() && str.equals("$")) { Expression unaryMinus = new Negation(nodes.pop()); nodes.push(unaryMinus); } else if(!nodes.isEmpty()){ Expression right = nodes.pop(); Expression binaryOperator = matchOperator(str, nodes.pop(), right); nodes.push(binaryOperator); } } if(!nodes.isEmpty()) root = nodes.pop(); return root; }
[ "public", "Expression", "constructTree", "(", "ArrayList", "<", "String", ">", "postTokens", ")", "{", "Expression", "root", "=", "null", ";", "Stack", "<", "Expression", ">", "nodes", "=", "new", "Stack", "<>", "(", ")", ";", "for", "(", "String", "str", ":", "postTokens", ")", "{", "if", "(", "str", ".", "isEmpty", "(", ")", ")", "{", "continue", ";", "}", "if", "(", "str", ".", "matches", "(", "\"[-+]?[0-9]*\\\\.?[0-9]+([eE][-+]?[0-9]+)?\"", ")", ")", "{", "nodes", ".", "push", "(", "new", "Constant", "(", "Double", ".", "parseDouble", "(", "str", ")", ")", ")", ";", "}", "else", "if", "(", "str", ".", "equals", "(", "var", ")", ")", "{", "nodes", ".", "push", "(", "new", "Variable", "(", "var", ")", ")", ";", "}", "else", "if", "(", "!", "nodes", ".", "isEmpty", "(", ")", "&&", "ExpressionParser", ".", "isFunction", "(", "str", ")", ")", "{", "Expression", "function", "=", "matchFunc", "(", "str", ",", "nodes", ".", "pop", "(", ")", ")", ";", "nodes", ".", "push", "(", "function", ")", ";", "}", "else", "if", "(", "!", "nodes", ".", "isEmpty", "(", ")", "&&", "str", ".", "equals", "(", "\"$\"", ")", ")", "{", "Expression", "unaryMinus", "=", "new", "Negation", "(", "nodes", ".", "pop", "(", ")", ")", ";", "nodes", ".", "push", "(", "unaryMinus", ")", ";", "}", "else", "if", "(", "!", "nodes", ".", "isEmpty", "(", ")", ")", "{", "Expression", "right", "=", "nodes", ".", "pop", "(", ")", ";", "Expression", "binaryOperator", "=", "matchOperator", "(", "str", ",", "nodes", ".", "pop", "(", ")", ",", "right", ")", ";", "nodes", ".", "push", "(", "binaryOperator", ")", ";", "}", "}", "if", "(", "!", "nodes", ".", "isEmpty", "(", ")", ")", "root", "=", "nodes", ".", "pop", "(", ")", ";", "return", "root", ";", "}" ]
reads the "tokens" in order from the list and builds a tree
[ "reads", "the", "tokens", "in", "order", "from", "the", "list", "and", "builds", "a", "tree" ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/symbolic/src/main/java/smile/symbolic/ExpressionTree.java#L163-L198
17,477
haifengl/smile
core/src/main/java/smile/classification/SVM.java
Trainer.train
public SVM<T> train(T[] x, int[] y, double[] weight) { SVM<T> svm = null; if (k == 2) { svm = new SVM<>(kernel, Cp, Cn); } else { if (this.weight == null) { svm = new SVM<>(kernel, Cp, k, strategy); } else { svm = new SVM<>(kernel, Cp, this.weight, strategy); } } svm.setTolerance(tol); for (int i = 1; i <= epochs; i++) { svm.learn(x, y, weight); } svm.finish(); return svm; }
java
public SVM<T> train(T[] x, int[] y, double[] weight) { SVM<T> svm = null; if (k == 2) { svm = new SVM<>(kernel, Cp, Cn); } else { if (this.weight == null) { svm = new SVM<>(kernel, Cp, k, strategy); } else { svm = new SVM<>(kernel, Cp, this.weight, strategy); } } svm.setTolerance(tol); for (int i = 1; i <= epochs; i++) { svm.learn(x, y, weight); } svm.finish(); return svm; }
[ "public", "SVM", "<", "T", ">", "train", "(", "T", "[", "]", "x", ",", "int", "[", "]", "y", ",", "double", "[", "]", "weight", ")", "{", "SVM", "<", "T", ">", "svm", "=", "null", ";", "if", "(", "k", "==", "2", ")", "{", "svm", "=", "new", "SVM", "<>", "(", "kernel", ",", "Cp", ",", "Cn", ")", ";", "}", "else", "{", "if", "(", "this", ".", "weight", "==", "null", ")", "{", "svm", "=", "new", "SVM", "<>", "(", "kernel", ",", "Cp", ",", "k", ",", "strategy", ")", ";", "}", "else", "{", "svm", "=", "new", "SVM", "<>", "(", "kernel", ",", "Cp", ",", "this", ".", "weight", ",", "strategy", ")", ";", "}", "}", "svm", ".", "setTolerance", "(", "tol", ")", ";", "for", "(", "int", "i", "=", "1", ";", "i", "<=", "epochs", ";", "i", "++", ")", "{", "svm", ".", "learn", "(", "x", ",", "y", ",", "weight", ")", ";", "}", "svm", ".", "finish", "(", ")", ";", "return", "svm", ";", "}" ]
Learns a SVM classifier with given training data. @param x training instances. @param y training labels in [0, k), where k is the number of classes. @param weight instance weight. Must be positive. The soft margin penalty for instance i will be weight[i] * C. @return trained SVM classifier
[ "Learns", "a", "SVM", "classifier", "with", "given", "training", "data", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/classification/SVM.java#L305-L324
17,478
haifengl/smile
core/src/main/java/smile/classification/NaiveBayes.java
NaiveBayes.learn
public void learn(double[][] x, int[] y) { if (model == Model.GENERAL) { throw new UnsupportedOperationException("General-mode Naive Bayes classifier doesn't support online learning."); } if (model == Model.MULTINOMIAL) { for (int i = 0; i < x.length; i++) { if (x[i].length != p) { throw new IllegalArgumentException("Invalid input vector size: " + x[i].length); } for (int j = 0; j < p; j++) { ntc[y[i]][j] += x[i][j]; nt[y[i]] += x[i][j]; } n++; nc[y[i]]++; } } else if (model == Model.POLYAURN) { for (int i = 0; i < x.length; i++) { if (x[i].length != p) { throw new IllegalArgumentException("Invalid input vector size: " + x[i].length); } for (int j = 0; j < p; j++) { ntc[y[i]][j] += x[i][j] * 2; nt[y[i]] += x[i][j] * 2; } n++; nc[y[i]]++; } } else { for (int i = 0; i < x.length; i++) { if (x[i].length != p) { throw new IllegalArgumentException("Invalid input vector size: " + x[i].length); } for (int j = 0; j < p; j++) { if (x[i][j] > 0) { ntc[y[i]][j]++; } } n++; nc[y[i]]++; } } update(); }
java
public void learn(double[][] x, int[] y) { if (model == Model.GENERAL) { throw new UnsupportedOperationException("General-mode Naive Bayes classifier doesn't support online learning."); } if (model == Model.MULTINOMIAL) { for (int i = 0; i < x.length; i++) { if (x[i].length != p) { throw new IllegalArgumentException("Invalid input vector size: " + x[i].length); } for (int j = 0; j < p; j++) { ntc[y[i]][j] += x[i][j]; nt[y[i]] += x[i][j]; } n++; nc[y[i]]++; } } else if (model == Model.POLYAURN) { for (int i = 0; i < x.length; i++) { if (x[i].length != p) { throw new IllegalArgumentException("Invalid input vector size: " + x[i].length); } for (int j = 0; j < p; j++) { ntc[y[i]][j] += x[i][j] * 2; nt[y[i]] += x[i][j] * 2; } n++; nc[y[i]]++; } } else { for (int i = 0; i < x.length; i++) { if (x[i].length != p) { throw new IllegalArgumentException("Invalid input vector size: " + x[i].length); } for (int j = 0; j < p; j++) { if (x[i][j] > 0) { ntc[y[i]][j]++; } } n++; nc[y[i]]++; } } update(); }
[ "public", "void", "learn", "(", "double", "[", "]", "[", "]", "x", ",", "int", "[", "]", "y", ")", "{", "if", "(", "model", "==", "Model", ".", "GENERAL", ")", "{", "throw", "new", "UnsupportedOperationException", "(", "\"General-mode Naive Bayes classifier doesn't support online learning.\"", ")", ";", "}", "if", "(", "model", "==", "Model", ".", "MULTINOMIAL", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "if", "(", "x", "[", "i", "]", ".", "length", "!=", "p", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Invalid input vector size: \"", "+", "x", "[", "i", "]", ".", "length", ")", ";", "}", "for", "(", "int", "j", "=", "0", ";", "j", "<", "p", ";", "j", "++", ")", "{", "ntc", "[", "y", "[", "i", "]", "]", "[", "j", "]", "+=", "x", "[", "i", "]", "[", "j", "]", ";", "nt", "[", "y", "[", "i", "]", "]", "+=", "x", "[", "i", "]", "[", "j", "]", ";", "}", "n", "++", ";", "nc", "[", "y", "[", "i", "]", "]", "++", ";", "}", "}", "else", "if", "(", "model", "==", "Model", ".", "POLYAURN", ")", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "if", "(", "x", "[", "i", "]", ".", "length", "!=", "p", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Invalid input vector size: \"", "+", "x", "[", "i", "]", ".", "length", ")", ";", "}", "for", "(", "int", "j", "=", "0", ";", "j", "<", "p", ";", "j", "++", ")", "{", "ntc", "[", "y", "[", "i", "]", "]", "[", "j", "]", "+=", "x", "[", "i", "]", "[", "j", "]", "*", "2", ";", "nt", "[", "y", "[", "i", "]", "]", "+=", "x", "[", "i", "]", "[", "j", "]", "*", "2", ";", "}", "n", "++", ";", "nc", "[", "y", "[", "i", "]", "]", "++", ";", "}", "}", "else", "{", "for", "(", "int", "i", "=", "0", ";", "i", "<", "x", ".", "length", ";", "i", "++", ")", "{", "if", "(", "x", "[", "i", "]", ".", "length", "!=", "p", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"Invalid input vector size: \"", "+", "x", "[", "i", "]", ".", "length", ")", ";", "}", "for", "(", "int", "j", "=", "0", ";", "j", "<", "p", ";", "j", "++", ")", "{", "if", "(", "x", "[", "i", "]", "[", "j", "]", ">", "0", ")", "{", "ntc", "[", "y", "[", "i", "]", "]", "[", "j", "]", "++", ";", "}", "}", "n", "++", ";", "nc", "[", "y", "[", "i", "]", "]", "++", ";", "}", "}", "update", "(", ")", ";", "}" ]
Online learning of naive Bayes classifier on sequences, which are modeled as a bag of words. Note that this method is NOT applicable for naive Bayes classifier with general generation model. @param x training instances. @param y training labels in [0, k), where k is the number of classes.
[ "Online", "learning", "of", "naive", "Bayes", "classifier", "on", "sequences", "which", "are", "modeled", "as", "a", "bag", "of", "words", ".", "Note", "that", "this", "method", "is", "NOT", "applicable", "for", "naive", "Bayes", "classifier", "with", "general", "generation", "model", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/classification/NaiveBayes.java#L551-L602
17,479
haifengl/smile
core/src/main/java/smile/classification/NaiveBayes.java
NaiveBayes.update
private void update() { if (!predefinedPriori) { for (int c = 0; c < k; c++) { priori[c] = (nc[c] + EPSILON) / (n + k * EPSILON); } } if (model == Model.MULTINOMIAL || model == Model.POLYAURN) { for (int c = 0; c < k; c++) { for (int t = 0; t < p; t++) { condprob[c][t] = (ntc[c][t] + sigma) / (nt[c] + sigma * p); } } } else { for (int c = 0; c < k; c++) { for (int t = 0; t < p; t++) { condprob[c][t] = (ntc[c][t] + sigma) / (nc[c] + sigma * 2); } } } }
java
private void update() { if (!predefinedPriori) { for (int c = 0; c < k; c++) { priori[c] = (nc[c] + EPSILON) / (n + k * EPSILON); } } if (model == Model.MULTINOMIAL || model == Model.POLYAURN) { for (int c = 0; c < k; c++) { for (int t = 0; t < p; t++) { condprob[c][t] = (ntc[c][t] + sigma) / (nt[c] + sigma * p); } } } else { for (int c = 0; c < k; c++) { for (int t = 0; t < p; t++) { condprob[c][t] = (ntc[c][t] + sigma) / (nc[c] + sigma * 2); } } } }
[ "private", "void", "update", "(", ")", "{", "if", "(", "!", "predefinedPriori", ")", "{", "for", "(", "int", "c", "=", "0", ";", "c", "<", "k", ";", "c", "++", ")", "{", "priori", "[", "c", "]", "=", "(", "nc", "[", "c", "]", "+", "EPSILON", ")", "/", "(", "n", "+", "k", "*", "EPSILON", ")", ";", "}", "}", "if", "(", "model", "==", "Model", ".", "MULTINOMIAL", "||", "model", "==", "Model", ".", "POLYAURN", ")", "{", "for", "(", "int", "c", "=", "0", ";", "c", "<", "k", ";", "c", "++", ")", "{", "for", "(", "int", "t", "=", "0", ";", "t", "<", "p", ";", "t", "++", ")", "{", "condprob", "[", "c", "]", "[", "t", "]", "=", "(", "ntc", "[", "c", "]", "[", "t", "]", "+", "sigma", ")", "/", "(", "nt", "[", "c", "]", "+", "sigma", "*", "p", ")", ";", "}", "}", "}", "else", "{", "for", "(", "int", "c", "=", "0", ";", "c", "<", "k", ";", "c", "++", ")", "{", "for", "(", "int", "t", "=", "0", ";", "t", "<", "p", ";", "t", "++", ")", "{", "condprob", "[", "c", "]", "[", "t", "]", "=", "(", "ntc", "[", "c", "]", "[", "t", "]", "+", "sigma", ")", "/", "(", "nc", "[", "c", "]", "+", "sigma", "*", "2", ")", ";", "}", "}", "}", "}" ]
Update conditional probabilities.
[ "Update", "conditional", "probabilities", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/classification/NaiveBayes.java#L607-L627
17,480
haifengl/smile
core/src/main/java/smile/classification/NaiveBayes.java
NaiveBayes.predict
@Override public int predict(double[] x, double[] posteriori) { if (x.length != p) { throw new IllegalArgumentException(String.format("Invalid input vector size: %d", x.length)); } if (posteriori != null && posteriori.length != k) { throw new IllegalArgumentException(String.format("Invalid posteriori vector size: %d, expected: %d", posteriori.length, k)); } int label = -1; double max = Double.NEGATIVE_INFINITY; boolean any = model == Model.GENERAL ? true : false; for (int i = 0; i < k; i++) { double logprob = Math.log(priori[i]); for (int j = 0; j < p; j++) { switch (model) { case GENERAL: logprob += prob[i][j].logp(x[j]); break; case MULTINOMIAL: case POLYAURN: if (x[j] > 0) { logprob += x[j] * Math.log(condprob[i][j]); any = true; } break; case BERNOULLI: if (x[j] > 0) { logprob += Math.log(condprob[i][j]); any = true; } else { logprob += Math.log(1.0 - condprob[i][j]); } break; } } if (logprob > max && any) { max = logprob; label = i; } if (posteriori != null) { posteriori[i] = logprob; } } if (posteriori != null && any) { double Z = 0.0; for (int i = 0; i < k; i++) { posteriori[i] = Math.exp(posteriori[i] - max); Z += posteriori[i]; } for (int i = 0; i < k; i++) { posteriori[i] /= Z; } } return label; }
java
@Override public int predict(double[] x, double[] posteriori) { if (x.length != p) { throw new IllegalArgumentException(String.format("Invalid input vector size: %d", x.length)); } if (posteriori != null && posteriori.length != k) { throw new IllegalArgumentException(String.format("Invalid posteriori vector size: %d, expected: %d", posteriori.length, k)); } int label = -1; double max = Double.NEGATIVE_INFINITY; boolean any = model == Model.GENERAL ? true : false; for (int i = 0; i < k; i++) { double logprob = Math.log(priori[i]); for (int j = 0; j < p; j++) { switch (model) { case GENERAL: logprob += prob[i][j].logp(x[j]); break; case MULTINOMIAL: case POLYAURN: if (x[j] > 0) { logprob += x[j] * Math.log(condprob[i][j]); any = true; } break; case BERNOULLI: if (x[j] > 0) { logprob += Math.log(condprob[i][j]); any = true; } else { logprob += Math.log(1.0 - condprob[i][j]); } break; } } if (logprob > max && any) { max = logprob; label = i; } if (posteriori != null) { posteriori[i] = logprob; } } if (posteriori != null && any) { double Z = 0.0; for (int i = 0; i < k; i++) { posteriori[i] = Math.exp(posteriori[i] - max); Z += posteriori[i]; } for (int i = 0; i < k; i++) { posteriori[i] /= Z; } } return label; }
[ "@", "Override", "public", "int", "predict", "(", "double", "[", "]", "x", ",", "double", "[", "]", "posteriori", ")", "{", "if", "(", "x", ".", "length", "!=", "p", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"Invalid input vector size: %d\"", ",", "x", ".", "length", ")", ")", ";", "}", "if", "(", "posteriori", "!=", "null", "&&", "posteriori", ".", "length", "!=", "k", ")", "{", "throw", "new", "IllegalArgumentException", "(", "String", ".", "format", "(", "\"Invalid posteriori vector size: %d, expected: %d\"", ",", "posteriori", ".", "length", ",", "k", ")", ")", ";", "}", "int", "label", "=", "-", "1", ";", "double", "max", "=", "Double", ".", "NEGATIVE_INFINITY", ";", "boolean", "any", "=", "model", "==", "Model", ".", "GENERAL", "?", "true", ":", "false", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "k", ";", "i", "++", ")", "{", "double", "logprob", "=", "Math", ".", "log", "(", "priori", "[", "i", "]", ")", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "p", ";", "j", "++", ")", "{", "switch", "(", "model", ")", "{", "case", "GENERAL", ":", "logprob", "+=", "prob", "[", "i", "]", "[", "j", "]", ".", "logp", "(", "x", "[", "j", "]", ")", ";", "break", ";", "case", "MULTINOMIAL", ":", "case", "POLYAURN", ":", "if", "(", "x", "[", "j", "]", ">", "0", ")", "{", "logprob", "+=", "x", "[", "j", "]", "*", "Math", ".", "log", "(", "condprob", "[", "i", "]", "[", "j", "]", ")", ";", "any", "=", "true", ";", "}", "break", ";", "case", "BERNOULLI", ":", "if", "(", "x", "[", "j", "]", ">", "0", ")", "{", "logprob", "+=", "Math", ".", "log", "(", "condprob", "[", "i", "]", "[", "j", "]", ")", ";", "any", "=", "true", ";", "}", "else", "{", "logprob", "+=", "Math", ".", "log", "(", "1.0", "-", "condprob", "[", "i", "]", "[", "j", "]", ")", ";", "}", "break", ";", "}", "}", "if", "(", "logprob", ">", "max", "&&", "any", ")", "{", "max", "=", "logprob", ";", "label", "=", "i", ";", "}", "if", "(", "posteriori", "!=", "null", ")", "{", "posteriori", "[", "i", "]", "=", "logprob", ";", "}", "}", "if", "(", "posteriori", "!=", "null", "&&", "any", ")", "{", "double", "Z", "=", "0.0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "k", ";", "i", "++", ")", "{", "posteriori", "[", "i", "]", "=", "Math", ".", "exp", "(", "posteriori", "[", "i", "]", "-", "max", ")", ";", "Z", "+=", "posteriori", "[", "i", "]", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "k", ";", "i", "++", ")", "{", "posteriori", "[", "i", "]", "/=", "Z", ";", "}", "}", "return", "label", ";", "}" ]
Predict the class of an instance. @param x the instance to be classified. @param posteriori the array to store a posteriori probabilities on output. @return the predicted class label. For MULTINOMIAL and BERNOULLI models, returns -1 if the instance does not contain any feature words.
[ "Predict", "the", "class", "of", "an", "instance", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/classification/NaiveBayes.java#L649-L711
17,481
haifengl/smile
math/src/main/java/smile/math/SparseArray.java
SparseArray.get
public double get(int i) { for (Entry e : array) { if (e.i == i) { return e.x; } } return 0.0; }
java
public double get(int i) { for (Entry e : array) { if (e.i == i) { return e.x; } } return 0.0; }
[ "public", "double", "get", "(", "int", "i", ")", "{", "for", "(", "Entry", "e", ":", "array", ")", "{", "if", "(", "e", ".", "i", "==", "i", ")", "{", "return", "e", ".", "x", ";", "}", "}", "return", "0.0", ";", "}" ]
Returns the value of i-th entry. @param i the index of entry. @return the value of entry, 0.0 if the index doesn't exist in the array.
[ "Returns", "the", "value", "of", "i", "-", "th", "entry", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/SparseArray.java#L104-L112
17,482
haifengl/smile
math/src/main/java/smile/math/SparseArray.java
SparseArray.set
public boolean set(int i, double x) { if (x == 0.0) { remove(i); return false; } Iterator<Entry> it = array.iterator(); for (int k = 0; it.hasNext(); k++) { Entry e = it.next(); if (e.i == i) { e.x = x; return false; } else if (e.i > i) { array.add(k, new Entry(i, x)); return true; } } array.add(new Entry(i, x)); return true; }
java
public boolean set(int i, double x) { if (x == 0.0) { remove(i); return false; } Iterator<Entry> it = array.iterator(); for (int k = 0; it.hasNext(); k++) { Entry e = it.next(); if (e.i == i) { e.x = x; return false; } else if (e.i > i) { array.add(k, new Entry(i, x)); return true; } } array.add(new Entry(i, x)); return true; }
[ "public", "boolean", "set", "(", "int", "i", ",", "double", "x", ")", "{", "if", "(", "x", "==", "0.0", ")", "{", "remove", "(", "i", ")", ";", "return", "false", ";", "}", "Iterator", "<", "Entry", ">", "it", "=", "array", ".", "iterator", "(", ")", ";", "for", "(", "int", "k", "=", "0", ";", "it", ".", "hasNext", "(", ")", ";", "k", "++", ")", "{", "Entry", "e", "=", "it", ".", "next", "(", ")", ";", "if", "(", "e", ".", "i", "==", "i", ")", "{", "e", ".", "x", "=", "x", ";", "return", "false", ";", "}", "else", "if", "(", "e", ".", "i", ">", "i", ")", "{", "array", ".", "add", "(", "k", ",", "new", "Entry", "(", "i", ",", "x", ")", ")", ";", "return", "true", ";", "}", "}", "array", ".", "add", "(", "new", "Entry", "(", "i", ",", "x", ")", ")", ";", "return", "true", ";", "}" ]
Sets or add an entry. @param i the index of entry. @param x the value of entry. @return true if a new entry added, false if an existing entry updated.
[ "Sets", "or", "add", "an", "entry", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/SparseArray.java#L120-L140
17,483
haifengl/smile
math/src/main/java/smile/math/SparseArray.java
SparseArray.remove
public void remove(int i) { Iterator<Entry> it = array.iterator(); while (it.hasNext()) { Entry e = it.next(); if (e.i == i) { it.remove(); break; } } }
java
public void remove(int i) { Iterator<Entry> it = array.iterator(); while (it.hasNext()) { Entry e = it.next(); if (e.i == i) { it.remove(); break; } } }
[ "public", "void", "remove", "(", "int", "i", ")", "{", "Iterator", "<", "Entry", ">", "it", "=", "array", ".", "iterator", "(", ")", ";", "while", "(", "it", ".", "hasNext", "(", ")", ")", "{", "Entry", "e", "=", "it", ".", "next", "(", ")", ";", "if", "(", "e", ".", "i", "==", "i", ")", "{", "it", ".", "remove", "(", ")", ";", "break", ";", "}", "}", "}" ]
Removes an entry. @param i the index of entry.
[ "Removes", "an", "entry", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/SparseArray.java#L157-L166
17,484
haifengl/smile
core/src/main/java/smile/regression/RandomForest.java
RandomForest.merge
public RandomForest merge(RandomForest other) { if (this.importance.length != other.importance.length) { throw new IllegalArgumentException("RandomForest have different sizes of feature vectors"); } ArrayList<RegressionTree> mergedTrees = new ArrayList<>(); mergedTrees.addAll(this.trees); mergedTrees.addAll(other.trees); double weightedMergedError = ((this.error * this.trees.size()) + (other.error * other.trees.size())) / (this.trees.size() + other.trees.size()); double[] mergedImportance = calculateImportance(mergedTrees, this.importance.length); return new RandomForest(mergedTrees, weightedMergedError, mergedImportance); }
java
public RandomForest merge(RandomForest other) { if (this.importance.length != other.importance.length) { throw new IllegalArgumentException("RandomForest have different sizes of feature vectors"); } ArrayList<RegressionTree> mergedTrees = new ArrayList<>(); mergedTrees.addAll(this.trees); mergedTrees.addAll(other.trees); double weightedMergedError = ((this.error * this.trees.size()) + (other.error * other.trees.size())) / (this.trees.size() + other.trees.size()); double[] mergedImportance = calculateImportance(mergedTrees, this.importance.length); return new RandomForest(mergedTrees, weightedMergedError, mergedImportance); }
[ "public", "RandomForest", "merge", "(", "RandomForest", "other", ")", "{", "if", "(", "this", ".", "importance", ".", "length", "!=", "other", ".", "importance", ".", "length", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"RandomForest have different sizes of feature vectors\"", ")", ";", "}", "ArrayList", "<", "RegressionTree", ">", "mergedTrees", "=", "new", "ArrayList", "<>", "(", ")", ";", "mergedTrees", ".", "addAll", "(", "this", ".", "trees", ")", ";", "mergedTrees", ".", "addAll", "(", "other", ".", "trees", ")", ";", "double", "weightedMergedError", "=", "(", "(", "this", ".", "error", "*", "this", ".", "trees", ".", "size", "(", ")", ")", "+", "(", "other", ".", "error", "*", "other", ".", "trees", ".", "size", "(", ")", ")", ")", "/", "(", "this", ".", "trees", ".", "size", "(", ")", "+", "other", ".", "trees", ".", "size", "(", ")", ")", ";", "double", "[", "]", "mergedImportance", "=", "calculateImportance", "(", "mergedTrees", ",", "this", ".", "importance", ".", "length", ")", ";", "return", "new", "RandomForest", "(", "mergedTrees", ",", "weightedMergedError", ",", "mergedImportance", ")", ";", "}" ]
Merges together two random forests and returns a new forest consisting of trees from both input forests.
[ "Merges", "together", "two", "random", "forests", "and", "returns", "a", "new", "forest", "consisting", "of", "trees", "from", "both", "input", "forests", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/regression/RandomForest.java#L585-L598
17,485
haifengl/smile
data/src/main/java/smile/data/parser/SparseMatrixParser.java
SparseMatrixParser.parse
public SparseMatrix parse(InputStream stream) throws IOException, ParseException { int nrows = 0, ncols = 0, n = 0; int[] colIndex; int[] rowIndex; double[] data; try (Scanner scanner = new Scanner(stream)) { String line = scanner.nextLine(); String[] tokens = line.split("\\s+"); if (tokens.length == 3) { try { nrows = Integer.parseInt(tokens[0]); ncols = Integer.parseInt(tokens[1]); n = Integer.parseInt(tokens[2]); } catch (Exception ex) { } } if (n == 0) { // Harwell-Boeing Exchange Format. We ignore first two lines. line = scanner.nextLine().trim(); tokens = line.split("\\s+"); int RHSCRD = Integer.parseInt(tokens[4]); line = scanner.nextLine().trim(); if (!line.startsWith("R")) { throw new UnsupportedOperationException("SparseMatrixParser supports only real-valued matrix."); } tokens = line.split("\\s+"); nrows = Integer.parseInt(tokens[1]); ncols = Integer.parseInt(tokens[2]); n = Integer.parseInt(tokens[3]); line = scanner.nextLine(); if (RHSCRD > 0) { line = scanner.nextLine(); } } colIndex = new int[ncols + 1]; rowIndex = new int[n]; data = new double[n]; for (int i = 0; i <= ncols; i++) { colIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < n; i++) { rowIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < n; i++) { data[i] = scanner.nextDouble(); } } SparseMatrix matrix = new SparseMatrix(nrows, ncols, data, rowIndex, colIndex); return matrix; }
java
public SparseMatrix parse(InputStream stream) throws IOException, ParseException { int nrows = 0, ncols = 0, n = 0; int[] colIndex; int[] rowIndex; double[] data; try (Scanner scanner = new Scanner(stream)) { String line = scanner.nextLine(); String[] tokens = line.split("\\s+"); if (tokens.length == 3) { try { nrows = Integer.parseInt(tokens[0]); ncols = Integer.parseInt(tokens[1]); n = Integer.parseInt(tokens[2]); } catch (Exception ex) { } } if (n == 0) { // Harwell-Boeing Exchange Format. We ignore first two lines. line = scanner.nextLine().trim(); tokens = line.split("\\s+"); int RHSCRD = Integer.parseInt(tokens[4]); line = scanner.nextLine().trim(); if (!line.startsWith("R")) { throw new UnsupportedOperationException("SparseMatrixParser supports only real-valued matrix."); } tokens = line.split("\\s+"); nrows = Integer.parseInt(tokens[1]); ncols = Integer.parseInt(tokens[2]); n = Integer.parseInt(tokens[3]); line = scanner.nextLine(); if (RHSCRD > 0) { line = scanner.nextLine(); } } colIndex = new int[ncols + 1]; rowIndex = new int[n]; data = new double[n]; for (int i = 0; i <= ncols; i++) { colIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < n; i++) { rowIndex[i] = scanner.nextInt() - 1; } for (int i = 0; i < n; i++) { data[i] = scanner.nextDouble(); } } SparseMatrix matrix = new SparseMatrix(nrows, ncols, data, rowIndex, colIndex); return matrix; }
[ "public", "SparseMatrix", "parse", "(", "InputStream", "stream", ")", "throws", "IOException", ",", "ParseException", "{", "int", "nrows", "=", "0", ",", "ncols", "=", "0", ",", "n", "=", "0", ";", "int", "[", "]", "colIndex", ";", "int", "[", "]", "rowIndex", ";", "double", "[", "]", "data", ";", "try", "(", "Scanner", "scanner", "=", "new", "Scanner", "(", "stream", ")", ")", "{", "String", "line", "=", "scanner", ".", "nextLine", "(", ")", ";", "String", "[", "]", "tokens", "=", "line", ".", "split", "(", "\"\\\\s+\"", ")", ";", "if", "(", "tokens", ".", "length", "==", "3", ")", "{", "try", "{", "nrows", "=", "Integer", ".", "parseInt", "(", "tokens", "[", "0", "]", ")", ";", "ncols", "=", "Integer", ".", "parseInt", "(", "tokens", "[", "1", "]", ")", ";", "n", "=", "Integer", ".", "parseInt", "(", "tokens", "[", "2", "]", ")", ";", "}", "catch", "(", "Exception", "ex", ")", "{", "}", "}", "if", "(", "n", "==", "0", ")", "{", "// Harwell-Boeing Exchange Format. We ignore first two lines.", "line", "=", "scanner", ".", "nextLine", "(", ")", ".", "trim", "(", ")", ";", "tokens", "=", "line", ".", "split", "(", "\"\\\\s+\"", ")", ";", "int", "RHSCRD", "=", "Integer", ".", "parseInt", "(", "tokens", "[", "4", "]", ")", ";", "line", "=", "scanner", ".", "nextLine", "(", ")", ".", "trim", "(", ")", ";", "if", "(", "!", "line", ".", "startsWith", "(", "\"R\"", ")", ")", "{", "throw", "new", "UnsupportedOperationException", "(", "\"SparseMatrixParser supports only real-valued matrix.\"", ")", ";", "}", "tokens", "=", "line", ".", "split", "(", "\"\\\\s+\"", ")", ";", "nrows", "=", "Integer", ".", "parseInt", "(", "tokens", "[", "1", "]", ")", ";", "ncols", "=", "Integer", ".", "parseInt", "(", "tokens", "[", "2", "]", ")", ";", "n", "=", "Integer", ".", "parseInt", "(", "tokens", "[", "3", "]", ")", ";", "line", "=", "scanner", ".", "nextLine", "(", ")", ";", "if", "(", "RHSCRD", ">", "0", ")", "{", "line", "=", "scanner", ".", "nextLine", "(", ")", ";", "}", "}", "colIndex", "=", "new", "int", "[", "ncols", "+", "1", "]", ";", "rowIndex", "=", "new", "int", "[", "n", "]", ";", "data", "=", "new", "double", "[", "n", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<=", "ncols", ";", "i", "++", ")", "{", "colIndex", "[", "i", "]", "=", "scanner", ".", "nextInt", "(", ")", "-", "1", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "rowIndex", "[", "i", "]", "=", "scanner", ".", "nextInt", "(", ")", "-", "1", ";", "}", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "data", "[", "i", "]", "=", "scanner", ".", "nextDouble", "(", ")", ";", "}", "}", "SparseMatrix", "matrix", "=", "new", "SparseMatrix", "(", "nrows", ",", "ncols", ",", "data", ",", "rowIndex", ",", "colIndex", ")", ";", "return", "matrix", ";", "}" ]
Parse a Harwell-Boeing column-compressed sparse matrix dataset from an input stream. @param stream the input stream of data. @throws java.io.IOException
[ "Parse", "a", "Harwell", "-", "Boeing", "column", "-", "compressed", "sparse", "matrix", "dataset", "from", "an", "input", "stream", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/SparseMatrixParser.java#L89-L145
17,486
haifengl/smile
math/src/main/java/smile/math/matrix/BiconjugateGradient.java
BiconjugateGradient.diagonalPreconditioner
private static Preconditioner diagonalPreconditioner(Matrix A) { return new Preconditioner() { public void asolve(double[] b, double[] x) { double[] diag = A.diag(); int n = diag.length; for (int i = 0; i < n; i++) { x[i] = diag[i] != 0.0 ? b[i] / diag[i] : b[i]; } } }; }
java
private static Preconditioner diagonalPreconditioner(Matrix A) { return new Preconditioner() { public void asolve(double[] b, double[] x) { double[] diag = A.diag(); int n = diag.length; for (int i = 0; i < n; i++) { x[i] = diag[i] != 0.0 ? b[i] / diag[i] : b[i]; } } }; }
[ "private", "static", "Preconditioner", "diagonalPreconditioner", "(", "Matrix", "A", ")", "{", "return", "new", "Preconditioner", "(", ")", "{", "public", "void", "asolve", "(", "double", "[", "]", "b", ",", "double", "[", "]", "x", ")", "{", "double", "[", "]", "diag", "=", "A", ".", "diag", "(", ")", ";", "int", "n", "=", "diag", ".", "length", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "x", "[", "i", "]", "=", "diag", "[", "i", "]", "!=", "0.0", "?", "b", "[", "i", "]", "/", "diag", "[", "i", "]", ":", "b", "[", "i", "]", ";", "}", "}", "}", ";", "}" ]
Returns a simple preconditioner matrix that is the trivial diagonal part of A in some cases.
[ "Returns", "a", "simple", "preconditioner", "matrix", "that", "is", "the", "trivial", "diagonal", "part", "of", "A", "in", "some", "cases", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/BiconjugateGradient.java#L35-L46
17,487
haifengl/smile
math/src/main/java/smile/math/matrix/BiconjugateGradient.java
BiconjugateGradient.snorm
private static double snorm(double[] x, int itol) { int n = x.length; if (itol <= 3) { double ans = 0.0; for (int i = 0; i < n; i++) { ans += x[i] * x[i]; } return Math.sqrt(ans); } else { int isamax = 0; for (int i = 0; i < n; i++) { if (Math.abs(x[i]) > Math.abs(x[isamax])) { isamax = i; } } return Math.abs(x[isamax]); } }
java
private static double snorm(double[] x, int itol) { int n = x.length; if (itol <= 3) { double ans = 0.0; for (int i = 0; i < n; i++) { ans += x[i] * x[i]; } return Math.sqrt(ans); } else { int isamax = 0; for (int i = 0; i < n; i++) { if (Math.abs(x[i]) > Math.abs(x[isamax])) { isamax = i; } } return Math.abs(x[isamax]); } }
[ "private", "static", "double", "snorm", "(", "double", "[", "]", "x", ",", "int", "itol", ")", "{", "int", "n", "=", "x", ".", "length", ";", "if", "(", "itol", "<=", "3", ")", "{", "double", "ans", "=", "0.0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "ans", "+=", "x", "[", "i", "]", "*", "x", "[", "i", "]", ";", "}", "return", "Math", ".", "sqrt", "(", "ans", ")", ";", "}", "else", "{", "int", "isamax", "=", "0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "if", "(", "Math", ".", "abs", "(", "x", "[", "i", "]", ")", ">", "Math", ".", "abs", "(", "x", "[", "isamax", "]", ")", ")", "{", "isamax", "=", "i", ";", "}", "}", "return", "Math", ".", "abs", "(", "x", "[", "isamax", "]", ")", ";", "}", "}" ]
Compute L2 or L-infinity norms for a vector x, as signaled by itol.
[ "Compute", "L2", "or", "L", "-", "infinity", "norms", "for", "a", "vector", "x", "as", "signaled", "by", "itol", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/BiconjugateGradient.java#L291-L310
17,488
haifengl/smile
demo/src/main/java/smile/demo/mds/SammonMappingDemo.java
SammonMappingDemo.learn
public JComponent learn() { JPanel pane = new JPanel(new GridLayout(1, 2)); double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]); String[] labels = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]); if (labels[0] == null) { Attribute[] attr = dataset[datasetIndex].attributes(); labels = new String[attr.length]; for (int i = 0; i < labels.length; i++) { labels[i] = attr[i].getName(); } } long clock = System.currentTimeMillis(); SammonMapping sammon = new SammonMapping(data, 2); System.out.format("Learn Sammon's Mapping (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis()-clock); PlotCanvas plot = ScatterPlot.plot(sammon.getCoordinates(), labels); plot.setTitle("Sammon's Mapping (k = 2)"); pane.add(plot); clock = System.currentTimeMillis(); sammon = new SammonMapping(data, 3); System.out.format("Learn Sammon's Mapping (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis()-clock); plot = ScatterPlot.plot(sammon.getCoordinates(), labels); plot.setTitle("Sammon's Mapping (k = 3)"); pane.add(plot); return pane; }
java
public JComponent learn() { JPanel pane = new JPanel(new GridLayout(1, 2)); double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]); String[] labels = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]); if (labels[0] == null) { Attribute[] attr = dataset[datasetIndex].attributes(); labels = new String[attr.length]; for (int i = 0; i < labels.length; i++) { labels[i] = attr[i].getName(); } } long clock = System.currentTimeMillis(); SammonMapping sammon = new SammonMapping(data, 2); System.out.format("Learn Sammon's Mapping (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis()-clock); PlotCanvas plot = ScatterPlot.plot(sammon.getCoordinates(), labels); plot.setTitle("Sammon's Mapping (k = 2)"); pane.add(plot); clock = System.currentTimeMillis(); sammon = new SammonMapping(data, 3); System.out.format("Learn Sammon's Mapping (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis()-clock); plot = ScatterPlot.plot(sammon.getCoordinates(), labels); plot.setTitle("Sammon's Mapping (k = 3)"); pane.add(plot); return pane; }
[ "public", "JComponent", "learn", "(", ")", "{", "JPanel", "pane", "=", "new", "JPanel", "(", "new", "GridLayout", "(", "1", ",", "2", ")", ")", ";", "double", "[", "]", "[", "]", "data", "=", "dataset", "[", "datasetIndex", "]", ".", "toArray", "(", "new", "double", "[", "dataset", "[", "datasetIndex", "]", ".", "size", "(", ")", "]", "[", "", "]", ")", ";", "String", "[", "]", "labels", "=", "dataset", "[", "datasetIndex", "]", ".", "toArray", "(", "new", "String", "[", "dataset", "[", "datasetIndex", "]", ".", "size", "(", ")", "]", ")", ";", "if", "(", "labels", "[", "0", "]", "==", "null", ")", "{", "Attribute", "[", "]", "attr", "=", "dataset", "[", "datasetIndex", "]", ".", "attributes", "(", ")", ";", "labels", "=", "new", "String", "[", "attr", ".", "length", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "labels", ".", "length", ";", "i", "++", ")", "{", "labels", "[", "i", "]", "=", "attr", "[", "i", "]", ".", "getName", "(", ")", ";", "}", "}", "long", "clock", "=", "System", ".", "currentTimeMillis", "(", ")", ";", "SammonMapping", "sammon", "=", "new", "SammonMapping", "(", "data", ",", "2", ")", ";", "System", ".", "out", ".", "format", "(", "\"Learn Sammon's Mapping (k=2) from %d samples in %dms\\n\"", ",", "data", ".", "length", ",", "System", ".", "currentTimeMillis", "(", ")", "-", "clock", ")", ";", "PlotCanvas", "plot", "=", "ScatterPlot", ".", "plot", "(", "sammon", ".", "getCoordinates", "(", ")", ",", "labels", ")", ";", "plot", ".", "setTitle", "(", "\"Sammon's Mapping (k = 2)\"", ")", ";", "pane", ".", "add", "(", "plot", ")", ";", "clock", "=", "System", ".", "currentTimeMillis", "(", ")", ";", "sammon", "=", "new", "SammonMapping", "(", "data", ",", "3", ")", ";", "System", ".", "out", ".", "format", "(", "\"Learn Sammon's Mapping (k=3) from %d samples in %dms\\n\"", ",", "data", ".", "length", ",", "System", ".", "currentTimeMillis", "(", ")", "-", "clock", ")", ";", "plot", "=", "ScatterPlot", ".", "plot", "(", "sammon", ".", "getCoordinates", "(", ")", ",", "labels", ")", ";", "plot", ".", "setTitle", "(", "\"Sammon's Mapping (k = 3)\"", ")", ";", "pane", ".", "add", "(", "plot", ")", ";", "return", "pane", ";", "}" ]
Execute the MDS algorithm and return a swing JComponent representing the clusters.
[ "Execute", "the", "MDS", "algorithm", "and", "return", "a", "swing", "JComponent", "representing", "the", "clusters", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/demo/src/main/java/smile/demo/mds/SammonMappingDemo.java#L95-L124
17,489
haifengl/smile
math/src/main/java/smile/math/DoubleArrayList.java
DoubleArrayList.add
public void add(double[] vals) { ensureCapacity(size + vals.length); System.arraycopy(vals, 0, data, size, vals.length); size += vals.length; }
java
public void add(double[] vals) { ensureCapacity(size + vals.length); System.arraycopy(vals, 0, data, size, vals.length); size += vals.length; }
[ "public", "void", "add", "(", "double", "[", "]", "vals", ")", "{", "ensureCapacity", "(", "size", "+", "vals", ".", "length", ")", ";", "System", ".", "arraycopy", "(", "vals", ",", "0", ",", "data", ",", "size", ",", "vals", ".", "length", ")", ";", "size", "+=", "vals", ".", "length", ";", "}" ]
Appends an array to the end of this list. @param vals an array to be appended to this list.
[ "Appends", "an", "array", "to", "the", "end", "of", "this", "list", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/DoubleArrayList.java#L126-L130
17,490
haifengl/smile
interpolation/src/main/java/smile/interpolation/LaplaceInterpolation.java
LaplaceInterpolation.snorm
private static double snorm(double[] sx) { int n = sx.length; double ans = 0.0; for (int i = 0; i < n; i++) { ans += sx[i] * sx[i]; } return Math.sqrt(ans); }
java
private static double snorm(double[] sx) { int n = sx.length; double ans = 0.0; for (int i = 0; i < n; i++) { ans += sx[i] * sx[i]; } return Math.sqrt(ans); }
[ "private", "static", "double", "snorm", "(", "double", "[", "]", "sx", ")", "{", "int", "n", "=", "sx", ".", "length", ";", "double", "ans", "=", "0.0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "ans", "+=", "sx", "[", "i", "]", "*", "sx", "[", "i", "]", ";", "}", "return", "Math", ".", "sqrt", "(", "ans", ")", ";", "}" ]
Compute squared root of L2 norms for a vector.
[ "Compute", "squared", "root", "of", "L2", "norms", "for", "a", "vector", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/interpolation/src/main/java/smile/interpolation/LaplaceInterpolation.java#L248-L256
17,491
haifengl/smile
core/src/main/java/smile/regression/SVR.java
SVR.gram
private void gram(SupportVector i) { int n = sv.size(); int m = MulticoreExecutor.getThreadPoolSize(); i.kcache = new DoubleArrayList(n); if (n < 100 || m < 2) { for (SupportVector v : sv) { i.kcache.add(kernel.k(i.x, v.x)); } } else { List<KernelTask> tasks = new ArrayList<>(m + 1); int step = n / m; if (step < 100) { step = 100; } int start = 0; int end = step; for (int l = 0; l < m-1; l++) { tasks.add(new KernelTask(i, start, end)); start += step; end += step; } tasks.add(new KernelTask(i, start, n)); try { for (double[] ki : MulticoreExecutor.run(tasks)) { for (double kij : ki) { i.kcache.add(kij); } } } catch (Exception ex) { for (SupportVector v : sv) { i.kcache.add(kernel.k(i.x, v.x)); } } } }
java
private void gram(SupportVector i) { int n = sv.size(); int m = MulticoreExecutor.getThreadPoolSize(); i.kcache = new DoubleArrayList(n); if (n < 100 || m < 2) { for (SupportVector v : sv) { i.kcache.add(kernel.k(i.x, v.x)); } } else { List<KernelTask> tasks = new ArrayList<>(m + 1); int step = n / m; if (step < 100) { step = 100; } int start = 0; int end = step; for (int l = 0; l < m-1; l++) { tasks.add(new KernelTask(i, start, end)); start += step; end += step; } tasks.add(new KernelTask(i, start, n)); try { for (double[] ki : MulticoreExecutor.run(tasks)) { for (double kij : ki) { i.kcache.add(kij); } } } catch (Exception ex) { for (SupportVector v : sv) { i.kcache.add(kernel.k(i.x, v.x)); } } } }
[ "private", "void", "gram", "(", "SupportVector", "i", ")", "{", "int", "n", "=", "sv", ".", "size", "(", ")", ";", "int", "m", "=", "MulticoreExecutor", ".", "getThreadPoolSize", "(", ")", ";", "i", ".", "kcache", "=", "new", "DoubleArrayList", "(", "n", ")", ";", "if", "(", "n", "<", "100", "||", "m", "<", "2", ")", "{", "for", "(", "SupportVector", "v", ":", "sv", ")", "{", "i", ".", "kcache", ".", "add", "(", "kernel", ".", "k", "(", "i", ".", "x", ",", "v", ".", "x", ")", ")", ";", "}", "}", "else", "{", "List", "<", "KernelTask", ">", "tasks", "=", "new", "ArrayList", "<>", "(", "m", "+", "1", ")", ";", "int", "step", "=", "n", "/", "m", ";", "if", "(", "step", "<", "100", ")", "{", "step", "=", "100", ";", "}", "int", "start", "=", "0", ";", "int", "end", "=", "step", ";", "for", "(", "int", "l", "=", "0", ";", "l", "<", "m", "-", "1", ";", "l", "++", ")", "{", "tasks", ".", "add", "(", "new", "KernelTask", "(", "i", ",", "start", ",", "end", ")", ")", ";", "start", "+=", "step", ";", "end", "+=", "step", ";", "}", "tasks", ".", "add", "(", "new", "KernelTask", "(", "i", ",", "start", ",", "n", ")", ")", ";", "try", "{", "for", "(", "double", "[", "]", "ki", ":", "MulticoreExecutor", ".", "run", "(", "tasks", ")", ")", "{", "for", "(", "double", "kij", ":", "ki", ")", "{", "i", ".", "kcache", ".", "add", "(", "kij", ")", ";", "}", "}", "}", "catch", "(", "Exception", "ex", ")", "{", "for", "(", "SupportVector", "v", ":", "sv", ")", "{", "i", ".", "kcache", ".", "add", "(", "kernel", ".", "k", "(", "i", ".", "x", ",", "v", ".", "x", ")", ")", ";", "}", "}", "}", "}" ]
Calculate the row of kernel matrix for a vector i. @param i data vector to evaluate kernel matrix.
[ "Calculate", "the", "row", "of", "kernel", "matrix", "for", "a", "vector", "i", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/regression/SVR.java#L412-L448
17,492
haifengl/smile
plot/src/main/java/smile/plot/Dendrogram.java
Dendrogram.dfs
private int dfs(int[][] merge, int index, int[] order, int i) { int n = merge.length + 1; if (merge[index][0] > merge.length) { i = dfs(merge, merge[index][0] - n, order, i); } else { order[i++] = merge[index][0]; } if (merge[index][1] > merge.length) { i = dfs(merge, merge[index][1] - n, order, i); } else { order[i++] = merge[index][1]; } return i; }
java
private int dfs(int[][] merge, int index, int[] order, int i) { int n = merge.length + 1; if (merge[index][0] > merge.length) { i = dfs(merge, merge[index][0] - n, order, i); } else { order[i++] = merge[index][0]; } if (merge[index][1] > merge.length) { i = dfs(merge, merge[index][1] - n, order, i); } else { order[i++] = merge[index][1]; } return i; }
[ "private", "int", "dfs", "(", "int", "[", "]", "[", "]", "merge", ",", "int", "index", ",", "int", "[", "]", "order", ",", "int", "i", ")", "{", "int", "n", "=", "merge", ".", "length", "+", "1", ";", "if", "(", "merge", "[", "index", "]", "[", "0", "]", ">", "merge", ".", "length", ")", "{", "i", "=", "dfs", "(", "merge", ",", "merge", "[", "index", "]", "[", "0", "]", "-", "n", ",", "order", ",", "i", ")", ";", "}", "else", "{", "order", "[", "i", "++", "]", "=", "merge", "[", "index", "]", "[", "0", "]", ";", "}", "if", "(", "merge", "[", "index", "]", "[", "1", "]", ">", "merge", ".", "length", ")", "{", "i", "=", "dfs", "(", "merge", ",", "merge", "[", "index", "]", "[", "1", "]", "-", "n", ",", "order", ",", "i", ")", ";", "}", "else", "{", "order", "[", "i", "++", "]", "=", "merge", "[", "index", "]", "[", "1", "]", ";", "}", "return", "i", ";", "}" ]
DFS the tree to find the order of leafs to avoid the cross of lines in the plot.
[ "DFS", "the", "tree", "to", "find", "the", "order", "of", "leafs", "to", "avoid", "the", "cross", "of", "lines", "in", "the", "plot", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Dendrogram.java#L118-L134
17,493
haifengl/smile
plot/src/main/java/smile/plot/Dendrogram.java
Dendrogram.plot
public static PlotCanvas plot(String id, int[][] merge, double[] height) { int n = merge.length + 1; Dendrogram dendrogram = new Dendrogram(merge, height); double[] lowerBound = {-n / 100, 0}; double[] upperBound = {n + n / 100, 1.01 * dendrogram.getHeight()}; PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound, false); canvas.getAxis(0).setGridVisible(false); canvas.getAxis(0).setLabelVisible(false); dendrogram.setID(id); canvas.add(dendrogram); return canvas; }
java
public static PlotCanvas plot(String id, int[][] merge, double[] height) { int n = merge.length + 1; Dendrogram dendrogram = new Dendrogram(merge, height); double[] lowerBound = {-n / 100, 0}; double[] upperBound = {n + n / 100, 1.01 * dendrogram.getHeight()}; PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound, false); canvas.getAxis(0).setGridVisible(false); canvas.getAxis(0).setLabelVisible(false); dendrogram.setID(id); canvas.add(dendrogram); return canvas; }
[ "public", "static", "PlotCanvas", "plot", "(", "String", "id", ",", "int", "[", "]", "[", "]", "merge", ",", "double", "[", "]", "height", ")", "{", "int", "n", "=", "merge", ".", "length", "+", "1", ";", "Dendrogram", "dendrogram", "=", "new", "Dendrogram", "(", "merge", ",", "height", ")", ";", "double", "[", "]", "lowerBound", "=", "{", "-", "n", "/", "100", ",", "0", "}", ";", "double", "[", "]", "upperBound", "=", "{", "n", "+", "n", "/", "100", ",", "1.01", "*", "dendrogram", ".", "getHeight", "(", ")", "}", ";", "PlotCanvas", "canvas", "=", "new", "PlotCanvas", "(", "lowerBound", ",", "upperBound", ",", "false", ")", ";", "canvas", ".", "getAxis", "(", "0", ")", ".", "setGridVisible", "(", "false", ")", ";", "canvas", ".", "getAxis", "(", "0", ")", ".", "setLabelVisible", "(", "false", ")", ";", "dendrogram", ".", "setID", "(", "id", ")", ";", "canvas", ".", "add", "(", "dendrogram", ")", ";", "return", "canvas", ";", "}" ]
Create a dendrogram plot. @param id the id of the plot. @param merge an n-1 by 2 matrix of which row i describes the merging of clusters at step i of the clustering. If an element j in the row is less than n, then observation j was merged at this stage. If j &ge; n then the merge was with the cluster formed at the (earlier) stage j-n of the algorithm. @param height a set of n-1 non-decreasing real values, which are the clustering height, i.e., the value of the criterion associated with the clustering method for the particular agglomeration.
[ "Create", "a", "dendrogram", "plot", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Dendrogram.java#L185-L200
17,494
haifengl/smile
data/src/main/java/smile/data/DateAttribute.java
DateAttribute.toDate
public Date toDate(double x) { if (Double.isNaN(x)) { return null; } return new Date(Double.doubleToRawLongBits(x)); }
java
public Date toDate(double x) { if (Double.isNaN(x)) { return null; } return new Date(Double.doubleToRawLongBits(x)); }
[ "public", "Date", "toDate", "(", "double", "x", ")", "{", "if", "(", "Double", ".", "isNaN", "(", "x", ")", ")", "{", "return", "null", ";", "}", "return", "new", "Date", "(", "Double", ".", "doubleToRawLongBits", "(", "x", ")", ")", ";", "}" ]
Retruns the date object from internal double encoding. @param x the date in double encoding.
[ "Retruns", "the", "date", "object", "from", "internal", "double", "encoding", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/DateAttribute.java#L113-L119
17,495
haifengl/smile
math/src/main/java/smile/math/matrix/JMatrix.java
JMatrix.cholesky
@Override public Cholesky cholesky() { if (nrows() != ncols()) { throw new UnsupportedOperationException("Cholesky decomposition on non-square matrix"); } int n = nrows(); // Main loop. for (int j = 0; j < n; j++) { double d = 0.0; for (int k = 0; k < j; k++) { double s = 0.0; for (int i = 0; i < k; i++) { s += get(k, i) * get(j, i); } s = (get(j, k) - s) / get(k, k); set(j, k, s); d = d + s * s; } d = get(j, j) - d; if (d < 0.0) { throw new IllegalArgumentException("The matrix is not positive definite."); } set(j, j, Math.sqrt(d)); } return new Cholesky(this); }
java
@Override public Cholesky cholesky() { if (nrows() != ncols()) { throw new UnsupportedOperationException("Cholesky decomposition on non-square matrix"); } int n = nrows(); // Main loop. for (int j = 0; j < n; j++) { double d = 0.0; for (int k = 0; k < j; k++) { double s = 0.0; for (int i = 0; i < k; i++) { s += get(k, i) * get(j, i); } s = (get(j, k) - s) / get(k, k); set(j, k, s); d = d + s * s; } d = get(j, j) - d; if (d < 0.0) { throw new IllegalArgumentException("The matrix is not positive definite."); } set(j, j, Math.sqrt(d)); } return new Cholesky(this); }
[ "@", "Override", "public", "Cholesky", "cholesky", "(", ")", "{", "if", "(", "nrows", "(", ")", "!=", "ncols", "(", ")", ")", "{", "throw", "new", "UnsupportedOperationException", "(", "\"Cholesky decomposition on non-square matrix\"", ")", ";", "}", "int", "n", "=", "nrows", "(", ")", ";", "// Main loop.", "for", "(", "int", "j", "=", "0", ";", "j", "<", "n", ";", "j", "++", ")", "{", "double", "d", "=", "0.0", ";", "for", "(", "int", "k", "=", "0", ";", "k", "<", "j", ";", "k", "++", ")", "{", "double", "s", "=", "0.0", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "k", ";", "i", "++", ")", "{", "s", "+=", "get", "(", "k", ",", "i", ")", "*", "get", "(", "j", ",", "i", ")", ";", "}", "s", "=", "(", "get", "(", "j", ",", "k", ")", "-", "s", ")", "/", "get", "(", "k", ",", "k", ")", ";", "set", "(", "j", ",", "k", ",", "s", ")", ";", "d", "=", "d", "+", "s", "*", "s", ";", "}", "d", "=", "get", "(", "j", ",", "j", ")", "-", "d", ";", "if", "(", "d", "<", "0.0", ")", "{", "throw", "new", "IllegalArgumentException", "(", "\"The matrix is not positive definite.\"", ")", ";", "}", "set", "(", "j", ",", "j", ",", "Math", ".", "sqrt", "(", "d", ")", ")", ";", "}", "return", "new", "Cholesky", "(", "this", ")", ";", "}" ]
Cholesky decomposition for symmetric and positive definite matrix. Only the lower triangular part will be used in the decomposition. @throws IllegalArgumentException if the matrix is not positive definite.
[ "Cholesky", "decomposition", "for", "symmetric", "and", "positive", "definite", "matrix", ".", "Only", "the", "lower", "triangular", "part", "will", "be", "used", "in", "the", "decomposition", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/JMatrix.java#L909-L939
17,496
haifengl/smile
math/src/main/java/smile/math/matrix/JMatrix.java
JMatrix.qr
@Override public QR qr() { // Initialize. int m = nrows(); int n = ncols(); double[] rDiagonal = new double[n]; // Main loop. for (int k = 0; k < n; k++) { // Compute 2-norm of k-th column without under/overflow. double nrm = 0.0; for (int i = k; i < m; i++) { nrm = Math.hypot(nrm, get(i, k)); } if (nrm != 0.0) { // Form k-th Householder vector. if (get(k, k) < 0) { nrm = -nrm; } for (int i = k; i < m; i++) { div(i, k, nrm); } add(k, k, 1.0); // Apply transformation to remaining columns. for (int j = k + 1; j < n; j++) { double s = 0.0; for (int i = k; i < m; i++) { s += get(i, k) * get(i, j); } s = -s / get(k, k); for (int i = k; i < m; i++) { add(i, j, s * get(i, k)); } } } rDiagonal[k] = -nrm; } boolean singular = false; for (int j = 0; j < rDiagonal.length; j++) { if (rDiagonal[j] == 0) { singular = true; break; } } return new QR(this, rDiagonal, singular); }
java
@Override public QR qr() { // Initialize. int m = nrows(); int n = ncols(); double[] rDiagonal = new double[n]; // Main loop. for (int k = 0; k < n; k++) { // Compute 2-norm of k-th column without under/overflow. double nrm = 0.0; for (int i = k; i < m; i++) { nrm = Math.hypot(nrm, get(i, k)); } if (nrm != 0.0) { // Form k-th Householder vector. if (get(k, k) < 0) { nrm = -nrm; } for (int i = k; i < m; i++) { div(i, k, nrm); } add(k, k, 1.0); // Apply transformation to remaining columns. for (int j = k + 1; j < n; j++) { double s = 0.0; for (int i = k; i < m; i++) { s += get(i, k) * get(i, j); } s = -s / get(k, k); for (int i = k; i < m; i++) { add(i, j, s * get(i, k)); } } } rDiagonal[k] = -nrm; } boolean singular = false; for (int j = 0; j < rDiagonal.length; j++) { if (rDiagonal[j] == 0) { singular = true; break; } } return new QR(this, rDiagonal, singular); }
[ "@", "Override", "public", "QR", "qr", "(", ")", "{", "// Initialize.", "int", "m", "=", "nrows", "(", ")", ";", "int", "n", "=", "ncols", "(", ")", ";", "double", "[", "]", "rDiagonal", "=", "new", "double", "[", "n", "]", ";", "// Main loop.", "for", "(", "int", "k", "=", "0", ";", "k", "<", "n", ";", "k", "++", ")", "{", "// Compute 2-norm of k-th column without under/overflow.", "double", "nrm", "=", "0.0", ";", "for", "(", "int", "i", "=", "k", ";", "i", "<", "m", ";", "i", "++", ")", "{", "nrm", "=", "Math", ".", "hypot", "(", "nrm", ",", "get", "(", "i", ",", "k", ")", ")", ";", "}", "if", "(", "nrm", "!=", "0.0", ")", "{", "// Form k-th Householder vector.", "if", "(", "get", "(", "k", ",", "k", ")", "<", "0", ")", "{", "nrm", "=", "-", "nrm", ";", "}", "for", "(", "int", "i", "=", "k", ";", "i", "<", "m", ";", "i", "++", ")", "{", "div", "(", "i", ",", "k", ",", "nrm", ")", ";", "}", "add", "(", "k", ",", "k", ",", "1.0", ")", ";", "// Apply transformation to remaining columns.", "for", "(", "int", "j", "=", "k", "+", "1", ";", "j", "<", "n", ";", "j", "++", ")", "{", "double", "s", "=", "0.0", ";", "for", "(", "int", "i", "=", "k", ";", "i", "<", "m", ";", "i", "++", ")", "{", "s", "+=", "get", "(", "i", ",", "k", ")", "*", "get", "(", "i", ",", "j", ")", ";", "}", "s", "=", "-", "s", "/", "get", "(", "k", ",", "k", ")", ";", "for", "(", "int", "i", "=", "k", ";", "i", "<", "m", ";", "i", "++", ")", "{", "add", "(", "i", ",", "j", ",", "s", "*", "get", "(", "i", ",", "k", ")", ")", ";", "}", "}", "}", "rDiagonal", "[", "k", "]", "=", "-", "nrm", ";", "}", "boolean", "singular", "=", "false", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "rDiagonal", ".", "length", ";", "j", "++", ")", "{", "if", "(", "rDiagonal", "[", "j", "]", "==", "0", ")", "{", "singular", "=", "true", ";", "break", ";", "}", "}", "return", "new", "QR", "(", "this", ",", "rDiagonal", ",", "singular", ")", ";", "}" ]
QR Decomposition is computed by Householder reflections.
[ "QR", "Decomposition", "is", "computed", "by", "Householder", "reflections", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/JMatrix.java#L944-L993
17,497
haifengl/smile
math/src/main/java/smile/math/matrix/JMatrix.java
JMatrix.balance
private static double[] balance(DenseMatrix A) { double sqrdx = Math.RADIX * Math.RADIX; int n = A.nrows(); double[] scale = new double[n]; for (int i = 0; i < n; i++) { scale[i] = 1.0; } boolean done = false; while (!done) { done = true; for (int i = 0; i < n; i++) { double r = 0.0, c = 0.0; for (int j = 0; j < n; j++) { if (j != i) { c += Math.abs(A.get(j, i)); r += Math.abs(A.get(i, j)); } } if (c != 0.0 && r != 0.0) { double g = r / Math.RADIX; double f = 1.0; double s = c + r; while (c < g) { f *= Math.RADIX; c *= sqrdx; } g = r * Math.RADIX; while (c > g) { f /= Math.RADIX; c /= sqrdx; } if ((c + r) / f < 0.95 * s) { done = false; g = 1.0 / f; scale[i] *= f; for (int j = 0; j < n; j++) { A.mul(i, j, g); } for (int j = 0; j < n; j++) { A.mul(j, i, f); } } } } } return scale; }
java
private static double[] balance(DenseMatrix A) { double sqrdx = Math.RADIX * Math.RADIX; int n = A.nrows(); double[] scale = new double[n]; for (int i = 0; i < n; i++) { scale[i] = 1.0; } boolean done = false; while (!done) { done = true; for (int i = 0; i < n; i++) { double r = 0.0, c = 0.0; for (int j = 0; j < n; j++) { if (j != i) { c += Math.abs(A.get(j, i)); r += Math.abs(A.get(i, j)); } } if (c != 0.0 && r != 0.0) { double g = r / Math.RADIX; double f = 1.0; double s = c + r; while (c < g) { f *= Math.RADIX; c *= sqrdx; } g = r * Math.RADIX; while (c > g) { f /= Math.RADIX; c /= sqrdx; } if ((c + r) / f < 0.95 * s) { done = false; g = 1.0 / f; scale[i] *= f; for (int j = 0; j < n; j++) { A.mul(i, j, g); } for (int j = 0; j < n; j++) { A.mul(j, i, f); } } } } } return scale; }
[ "private", "static", "double", "[", "]", "balance", "(", "DenseMatrix", "A", ")", "{", "double", "sqrdx", "=", "Math", ".", "RADIX", "*", "Math", ".", "RADIX", ";", "int", "n", "=", "A", ".", "nrows", "(", ")", ";", "double", "[", "]", "scale", "=", "new", "double", "[", "n", "]", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "scale", "[", "i", "]", "=", "1.0", ";", "}", "boolean", "done", "=", "false", ";", "while", "(", "!", "done", ")", "{", "done", "=", "true", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "double", "r", "=", "0.0", ",", "c", "=", "0.0", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "n", ";", "j", "++", ")", "{", "if", "(", "j", "!=", "i", ")", "{", "c", "+=", "Math", ".", "abs", "(", "A", ".", "get", "(", "j", ",", "i", ")", ")", ";", "r", "+=", "Math", ".", "abs", "(", "A", ".", "get", "(", "i", ",", "j", ")", ")", ";", "}", "}", "if", "(", "c", "!=", "0.0", "&&", "r", "!=", "0.0", ")", "{", "double", "g", "=", "r", "/", "Math", ".", "RADIX", ";", "double", "f", "=", "1.0", ";", "double", "s", "=", "c", "+", "r", ";", "while", "(", "c", "<", "g", ")", "{", "f", "*=", "Math", ".", "RADIX", ";", "c", "*=", "sqrdx", ";", "}", "g", "=", "r", "*", "Math", ".", "RADIX", ";", "while", "(", "c", ">", "g", ")", "{", "f", "/=", "Math", ".", "RADIX", ";", "c", "/=", "sqrdx", ";", "}", "if", "(", "(", "c", "+", "r", ")", "/", "f", "<", "0.95", "*", "s", ")", "{", "done", "=", "false", ";", "g", "=", "1.0", "/", "f", ";", "scale", "[", "i", "]", "*=", "f", ";", "for", "(", "int", "j", "=", "0", ";", "j", "<", "n", ";", "j", "++", ")", "{", "A", ".", "mul", "(", "i", ",", "j", ",", "g", ")", ";", "}", "for", "(", "int", "j", "=", "0", ";", "j", "<", "n", ";", "j", "++", ")", "{", "A", ".", "mul", "(", "j", ",", "i", ",", "f", ")", ";", "}", "}", "}", "}", "}", "return", "scale", ";", "}" ]
Given a square matrix, this routine replaces it by a balanced matrix with identical eigenvalues. A symmetric matrix is already balanced and is unaffected by this procedure.
[ "Given", "a", "square", "matrix", "this", "routine", "replaces", "it", "by", "a", "balanced", "matrix", "with", "identical", "eigenvalues", ".", "A", "symmetric", "matrix", "is", "already", "balanced", "and", "is", "unaffected", "by", "this", "procedure", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/JMatrix.java#L1815-L1865
17,498
haifengl/smile
math/src/main/java/smile/math/matrix/JMatrix.java
JMatrix.balbak
private static void balbak(DenseMatrix V, double[] scale) { int n = V.nrows(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { V.mul(i, j, scale[i]); } } }
java
private static void balbak(DenseMatrix V, double[] scale) { int n = V.nrows(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { V.mul(i, j, scale[i]); } } }
[ "private", "static", "void", "balbak", "(", "DenseMatrix", "V", ",", "double", "[", "]", "scale", ")", "{", "int", "n", "=", "V", ".", "nrows", "(", ")", ";", "for", "(", "int", "i", "=", "0", ";", "i", "<", "n", ";", "i", "++", ")", "{", "for", "(", "int", "j", "=", "0", ";", "j", "<", "n", ";", "j", "++", ")", "{", "V", ".", "mul", "(", "i", ",", "j", ",", "scale", "[", "i", "]", ")", ";", "}", "}", "}" ]
Form the eigenvectors of a real nonsymmetric matrix by back transforming those of the corresponding balanced matrix determined by balance.
[ "Form", "the", "eigenvectors", "of", "a", "real", "nonsymmetric", "matrix", "by", "back", "transforming", "those", "of", "the", "corresponding", "balanced", "matrix", "determined", "by", "balance", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/JMatrix.java#L1871-L1878
17,499
haifengl/smile
math/src/main/java/smile/math/matrix/JMatrix.java
JMatrix.elmhes
private static int[] elmhes(DenseMatrix A) { int n = A.nrows(); int[] perm = new int[n]; for (int m = 1; m < n - 1; m++) { double x = 0.0; int i = m; for (int j = m; j < n; j++) { if (Math.abs(A.get(j, m - 1)) > Math.abs(x)) { x = A.get(j, m - 1); i = j; } } perm[m] = i; if (i != m) { for (int j = m - 1; j < n; j++) { double swap = A.get(i, j); A.set(i, j, A.get(m, j)); A.set(m, j, swap); } for (int j = 0; j < n; j++) { double swap = A.get(j, i); A.set(j, i, A.get(j, m)); A.set(j, m, swap); } } if (x != 0.0) { for (i = m + 1; i < n; i++) { double y = A.get(i, m - 1); if (y != 0.0) { y /= x; A.set(i, m - 1, y); for (int j = m; j < n; j++) { A.sub(i, j, y * A.get(m, j)); } for (int j = 0; j < n; j++) { A.add(j, m, y * A.get(j, i)); } } } } } return perm; }
java
private static int[] elmhes(DenseMatrix A) { int n = A.nrows(); int[] perm = new int[n]; for (int m = 1; m < n - 1; m++) { double x = 0.0; int i = m; for (int j = m; j < n; j++) { if (Math.abs(A.get(j, m - 1)) > Math.abs(x)) { x = A.get(j, m - 1); i = j; } } perm[m] = i; if (i != m) { for (int j = m - 1; j < n; j++) { double swap = A.get(i, j); A.set(i, j, A.get(m, j)); A.set(m, j, swap); } for (int j = 0; j < n; j++) { double swap = A.get(j, i); A.set(j, i, A.get(j, m)); A.set(j, m, swap); } } if (x != 0.0) { for (i = m + 1; i < n; i++) { double y = A.get(i, m - 1); if (y != 0.0) { y /= x; A.set(i, m - 1, y); for (int j = m; j < n; j++) { A.sub(i, j, y * A.get(m, j)); } for (int j = 0; j < n; j++) { A.add(j, m, y * A.get(j, i)); } } } } } return perm; }
[ "private", "static", "int", "[", "]", "elmhes", "(", "DenseMatrix", "A", ")", "{", "int", "n", "=", "A", ".", "nrows", "(", ")", ";", "int", "[", "]", "perm", "=", "new", "int", "[", "n", "]", ";", "for", "(", "int", "m", "=", "1", ";", "m", "<", "n", "-", "1", ";", "m", "++", ")", "{", "double", "x", "=", "0.0", ";", "int", "i", "=", "m", ";", "for", "(", "int", "j", "=", "m", ";", "j", "<", "n", ";", "j", "++", ")", "{", "if", "(", "Math", ".", "abs", "(", "A", ".", "get", "(", "j", ",", "m", "-", "1", ")", ")", ">", "Math", ".", "abs", "(", "x", ")", ")", "{", "x", "=", "A", ".", "get", "(", "j", ",", "m", "-", "1", ")", ";", "i", "=", "j", ";", "}", "}", "perm", "[", "m", "]", "=", "i", ";", "if", "(", "i", "!=", "m", ")", "{", "for", "(", "int", "j", "=", "m", "-", "1", ";", "j", "<", "n", ";", "j", "++", ")", "{", "double", "swap", "=", "A", ".", "get", "(", "i", ",", "j", ")", ";", "A", ".", "set", "(", "i", ",", "j", ",", "A", ".", "get", "(", "m", ",", "j", ")", ")", ";", "A", ".", "set", "(", "m", ",", "j", ",", "swap", ")", ";", "}", "for", "(", "int", "j", "=", "0", ";", "j", "<", "n", ";", "j", "++", ")", "{", "double", "swap", "=", "A", ".", "get", "(", "j", ",", "i", ")", ";", "A", ".", "set", "(", "j", ",", "i", ",", "A", ".", "get", "(", "j", ",", "m", ")", ")", ";", "A", ".", "set", "(", "j", ",", "m", ",", "swap", ")", ";", "}", "}", "if", "(", "x", "!=", "0.0", ")", "{", "for", "(", "i", "=", "m", "+", "1", ";", "i", "<", "n", ";", "i", "++", ")", "{", "double", "y", "=", "A", ".", "get", "(", "i", ",", "m", "-", "1", ")", ";", "if", "(", "y", "!=", "0.0", ")", "{", "y", "/=", "x", ";", "A", ".", "set", "(", "i", ",", "m", "-", "1", ",", "y", ")", ";", "for", "(", "int", "j", "=", "m", ";", "j", "<", "n", ";", "j", "++", ")", "{", "A", ".", "sub", "(", "i", ",", "j", ",", "y", "*", "A", ".", "get", "(", "m", ",", "j", ")", ")", ";", "}", "for", "(", "int", "j", "=", "0", ";", "j", "<", "n", ";", "j", "++", ")", "{", "A", ".", "add", "(", "j", ",", "m", ",", "y", "*", "A", ".", "get", "(", "j", ",", "i", ")", ")", ";", "}", "}", "}", "}", "}", "return", "perm", ";", "}" ]
Reduce a real nonsymmetric matrix to upper Hessenberg form.
[ "Reduce", "a", "real", "nonsymmetric", "matrix", "to", "upper", "Hessenberg", "form", "." ]
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/JMatrix.java#L1883-L1927