4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rで代数的集合をプロットする

Last updated at Posted at 2016-11-03

Watanabe理論勉強会というのやっているのですが、スライドの中にある代数的集合のプロットはRを使って描いています。

多項式ごとにひとつひとつ描いていたのですが、面倒になってきたので関数化してみます。

R
plot_algebraic_set <- function(f, xlim = c(-1, 1), ylim = c(-1, 1), 
                               main = NULL, lwd = 2, ...) {
  if(is.null(main)) {
    main <- parse(text = sprintf('paste(%s, " = 0")', deparse(body(f))))
  }
  x_range <- seq(xlim[1], xlim[2], length.out = 100)
  y_range <- seq(ylim[1], ylim[2], length.out = 100)
  z <- vapply(y_range, function(y) {
    vapply(x_range, f, numeric(1), y = y)
  }, numeric(100))
  contour(x_range, y_range, z, nlevels = 1, method = "edge",
          xlim = xlim, ylim = ylim, main = main, lwd = lwd, ...)
}

これを使うと、多項式に対する代数的集合が簡単に描画できます。1

スライドにある Example 1 から 5 までを描画してみましょう。

R
# Example 1
f1 <- function(x, y) y - x^3
plot_algebraic_set(f1, xlim = c(-2, 2), ylim = c(-3, 3))

example1.png

R
# Example 3
f3 <- function(x, y) x * y
plot_algebraic_set(f3, main = "xy = 0")

example3.png

R
# Example 4
f4 <- function(x, y) y^2 - x^3
plot_algebraic_set(f4, xlim = c(-0.5, 2), ylim = c(-3, 3))

example4.png

R
f5 <- function(x, y) x^5 - y^3
plot_algebraic_set(f5)

example5.png

同じ図が描けました。

Enjoy!

  1. ただし、2次元限定です。

4
5
0

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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?