LoginSignup
7
7

More than 5 years have passed since last update.

第1回 数学をコードにする試み(∈編)

Last updated at Posted at 2017-02-20

この投稿は、Mathematics to code : ∈ - by Kohei Shingai の日本語訳です。

is.element.of.2.jpg

is.element.of.1.jpg
https://www.instagram.com/p/BQvOfRygJCA

Rの関数で、x ∈ A を確認する

is.element(x, A) を使う、

A <- c(1:5)
# 集合A は、 [1, 2, 3, 4, 5]

is.element(1, A) # 結果: TRUE
# つまり、 1 ∈ A - {1, 2, 3, 4, 5} かどうか? 

is.element(6, A) # 結果: FALSE
# つまり、 6 ∈ A - {1, 2, 3, 4, 5} かどうか?

Pythonの標準演算子で、x ∈ A を確認する

x in A を使う、

A = set([1, 2, 3, 4, 5]) # 集合A

1 in A # 結果: True
# つまり、 1 ∈ A - {1, 2, 3, 4, 5} かどうか?

6 in A # 結果: False
# つまり、 6 ∈ A - {1, 2, 3, 4, 5} かどうか?

Rubyのメソッドで、x ∈ A を確認する

A.include?x を使う、

A = (1..5).to_a
# 集合A は、 [1, 2, 3, 4, 5]

A.include?1 # 結果: true
# つまり、 1 ∈ A - {1, 2, 3, 4, 5} かどうか?

A.include?6 # 結果: false
# つまり、 6 ∈ A - {1, 2, 3, 4, 5} かどうか?

JavaScriptの式やメソッドで、x ∈ A を確認する

A.indexOf() != -1 もしくは A.includes(x) を使う、

A = [1, 2, 3, 4, 5] // 集合A

A.indexOf(1) != -1 // 結果: true
// つまり、 1 ∈ A - {1, 2, 3, 4, 5} かどうか?

A.indexOf(6) != -1 // 結果: false
// つまり、 6 ∈ A - {1, 2, 3, 4, 5} かどうか?

A.includes(1) // 結果: true - ES2016以降
// つまり、 1 ∈ A - {1, 2, 3, 4, 5} かどうか?

A.includes(6) // 結果: false - ES2016以降
// つまり、 6 ∈ A - {1, 2, 3, 4, 5} かどうか?
7
7
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7