Python、
Python、
組み組み<wbr>合わせた<wbr>結果の<wbr>要素数を<wbr>返す
、計算結果のみを<wbr>返す
[TOC]
Python
Python に
itertools、 math を 使用する 例
numpy, scipy.special, scipy.misc を 使用する 例
組み<wbr>合わせた<wbr>結果の<wbr>要素数を<wbr>返す
のが計算結果のみを<wbr>返す
のが
JavaScript
Vanilla JS での 実行
自作関数での、
* JavaScriptで
* JSで
Math.js を 使う
math.js | an extensive math library for JavaScript and Node.js と
以下、
Chrome console で Math.js を 読み込む
cdn から
var ele = document.createElement("script"); ele.type = "text/javascript"; ele.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.1.1/math.min.js"; document.body.appendChild(ele);
計算結果のみを<wbr>返す
- 階乗、
順列
math.permutations
で階乗、 順列 の 計算が できます。
階乗の計算は、 以下の 通り、 math.permutations(4);
math.factorial
でも階乗の 計算が できます。 順列のmath.factorial(4);
計算は、 以下の 通りです。 math.permutations(4,2);
- 組み
合わせ
math.combinations
で組み合わせの 計算が できます。 母数よりも、// 5個から3個を選ぶ場合の組み合わせ数 math.combinations(5, 3);
取り出す 数が 多いと エラーに なります。 math.combinations(3, 5); ----------- math.js:13664 Uncaught TypeError: k must be less than or equal to n at number, number (math.js:13664) at Object.combinations (math.js:26193) at <anonymous>:1:6 -----------
組み合わせた<wbr>結果の<wbr>要素数を<wbr>返す
Set Function
で
math.setCartesian(set1, set2)
JSON.stringify(math.setCartesian([1, 2, 3], [3, 4])); // retrun "[[1,3],[1,4],[2,3],[2,4],[3,3],[3,4]]"
math.setPowerset(set)
JSON.stringify(math.setPowerset([2,3,54])); // retrun "[[],[2],[3],[54],[2,3],[2,54],[3,54],[2,3,54]]"
python の itertools と 同様の 処理を 行う
以下の
Java
Apache commons-math3 を 使う
apache/commons-math: Mirror of Apache Commons Math で
pom.xml の
依存定義 <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> <version>3.5</version> </dependency>
階乗の
計算
CombinatoricsUtils.factorial()
を使用すると、 階乗の 計算が できます。 import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.math3.util.CombinatoricsUtils; public class FactorialExample { public static void main(String[] args) { // 階乗の計算 long result = CombinatoricsUtils.factorial(5); System.out.println("Outputs of CombinatoricsUtils#factorial()..."); System.out.println(ReflectionToStringBuilder.toString(result)); } }
Outputs of CombinatoricsUtils#factorial()... java.lang.Long@63961c42[value=120]
- 組み
合わせの 計算
Combinations
、CombinatoricsUtils#combinationsIterator
で組み合わせの 計算が 可能です。
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.math3.util.Combinations; import org.apache.commons.math3.util.CombinatoricsUtils; import java.util.*; /** * CombinationsExample */ public class CombinationsExample { public static void main(String[] args) { Combinations c = new Combinations(5, 3); List<int[]> al = new ArrayList<>(); for (int[] iterate : c) { al.add(iterate); } Collections.sort(al, Comparator.comparing((int[] arr) -> arr[0])); System.out.println("Outputs of combinations..."); for (int[] array : al) { System.out.println(ReflectionToStringBuilder.toString(array)); } Iterator<int[]> itr = CombinatoricsUtils.combinationsIterator(5, 3); System.out.println("Outputs of CombinatoricsUtils#combinationsIterator..."); for (Iterator<int[]> it = itr; it.hasNext(); ) { int[] array = it.next(); System.out.println(ReflectionToStringBuilder.toString(array)); } } }
- 順列の
計算
commons-math3 で順列の 計算が 可能な メソッドは 見つけられませんでした。
CombinatoricsUtils
では、他に 2項分布 の 計算を 行う メソッド、 第2スターリング数の 計算を 行う メソッドが あります。
順列は結構一般的に 思うのですが、 どうなんでしょうか。
参考
以下、
- Math.js: 多機能JavaScript Math
ライブラリ - 【Java】
組合せの 数を 求める - ヽ|∵|ゝ(Fantom) の 開発blog? - commons-math 解読 (1) : MathUtils に
定義されている static メソッド〜 整数関数編〜 - 倭算数理研究所 - スターリング数 - Wikipedia
- CombinatoricsUtils (Apache Commons Math 3.3 API)
以上です。
コメント