R 中的运算符重载和类定义:使用不同的基本字段/语料库

发布于 2024-12-14 01:22:50 字数 376 浏览 0 评论 0原文

(我使用“字段”这个词数学意义上的;基本字段R 已经使用的 /corpora 包括实数和复数。)

我有兴趣允许一些其他基本字段/语料库(例如 F₅,它是基本的模块化算术) 5)。为此,我需要

  1. 定义一个新的数据类型,
  2. 重载相关运算符(+*,也许还有更多),
  3. 也许还有其他操作?例如,与其他功能集成?

那么,如何在 R 中定义新的数据类型或重载运算符呢?

(I'm using the word "field" in the mathematical sense; base fields/corpora which R already uses include the real and complex numbers.)

I'm interested in allowing some other base fields/corpora (like F₅, which is modular arithmetic in base 5). To do that I would need to

  1. define a new data type
  2. overload the relevant operators (+, *, and maybe more)
  3. maybe something else? e.g., integrate with other functionality?

So, how does one define a new data type or overload operators in R?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-12-21 01:22:51

我对你的问题的解释与@Andrie有点不同,但他已经完成了一堆所需的S3类工作。我以为您想在具有五个元素或可能是一个环的群上开发群运算。然后,您可能需要一个带有单位元素 == 0 的“+”操作,也许需要一个带有单位元素 == 1 的“*”操作。

如果您希望将非负整数映射到此,您可以使用模算术运算符 < code>%% 或许还有 %/%

?Ops
as.g5 <- function(x){
  if(!inherits(x, "g5")) class(x) <- c("g5", class(x))
  x %% 5
}

print.g5 <- function(x, ...){

  cat("G5 equivalent:\n")
  cat(x %% 5)
  invisible(x)
}

如果您想要两个运算符,您可能正在寻找:

 `+.g5` <- function(e1, e2){
   NextMethod(e1 ,e2) %% 5
 }

 `*.g5` <- function(e1, e2){
   NextMethod(e1 ,e2) %% 5
 }
 x <- as.g5(0:10)
 y <- as.g5(5)

 x + y
#G5 equivalent:
#0 1 2 3 4 0 1 2 3 4 0
 y <- as.g5(2)
 x * y
#G5 equivalent:
#0 2 4 1 3 0 2 4 1 3 0

也可以在向量的“易失性”版本上使用这些操作:

 as.g5(1:10) * as.g5(1:10)
# G5 equivalent:
# 1 4 4 1 0 1 4 4 1 0

I interpreted your question a bit differently than @Andrie, but he has already done a bunch of the needed S3 class work. I thought you wanted to develop group operations on a group with five elements, or perhaps a ring. You would then want a "+" operation with an identity element == 0 and perhaps a "*" operation with an identity element == 1.

If you wanted the nonnegative integers mapped into this, you would use the modulo arithmetic operators, %% and perhaps %/%:

?Ops
as.g5 <- function(x){
  if(!inherits(x, "g5")) class(x) <- c("g5", class(x))
  x %% 5
}

print.g5 <- function(x, ...){

  cat("G5 equivalent:\n")
  cat(x %% 5)
  invisible(x)
}

If you wanted two operators you might be looking for:

 `+.g5` <- function(e1, e2){
   NextMethod(e1 ,e2) %% 5
 }

 `*.g5` <- function(e1, e2){
   NextMethod(e1 ,e2) %% 5
 }
 x <- as.g5(0:10)
 y <- as.g5(5)

 x + y
#G5 equivalent:
#0 1 2 3 4 0 1 2 3 4 0
 y <- as.g5(2)
 x * y
#G5 equivalent:
#0 2 4 1 3 0 2 4 1 3 0

It's also possible to use these operation on "volatile" versions of vectors:

 as.g5(1:10) * as.g5(1:10)
# G5 equivalent:
# 1 4 4 1 0 1 4 4 1 0
み零 2024-12-21 01:22:50

我发现 Hadley Wickham 的 devtools wiki 是开始使用 R 类的宝贵资源。特别是,请阅读以下部分:

  • < a href="https://github.com/hadley/devtools/wiki/S3">S3 类
  • S4类

这是一个开始说明 S3 类中的一些概念的点。我们将新类命名为 f5。至少,您可能希望为以下内容创建方法:

  • 强制:as.f5
  • 测试:is.f5
  • 一些基本运算符:+.f5 >
  • 处理打印的类: print.f5

一些代码(使用 GLDEX 包中的 digitsBase 进行基数转换):

library(GLDEX)

as.f5 <- function(x){
  if(!inherits(x, "f5")) class(x) <- c("f5", class(x))
  x
}

is.f5 <- function(x){
  inherits(x, "f5")
}

`+.f5` <- function(e1, e2){
  NextMethod(e1, e2)
}

print.f5 <- function(x, ...){
  # Next line from ?GLDEX::digitsBase
  b2ch <- function(db) noquote(gsub("^0+(.{1,})$"," \1", 
                           apply(db, 2, paste, collapse = "")))

  cat("Base 5:\n")
  cat(b2ch(digitsBase(x, 5)))
  invisible(x)
}


x <- as.f5(0:10)
y <- as.f5(5)

x + y

Base 5:
10 11 12 13 14 20 21 22 23 24 30

I found Hadley Wickham's devtools wiki an invaluable resource for getting started with classes in R. In particular, read the sections on:

Here is a starting point that illustrates some of the concepts in S3 classes. Let's call your new class f5. At a minimum, you would probably want to create methods for:

  • Coercion: as.f5
  • Test: is.f5
  • Some basic operators: +.f5
  • A class to handle printing: print.f5

Some code (using digitsBase in package GLDEX to do the base conversion):

library(GLDEX)

as.f5 <- function(x){
  if(!inherits(x, "f5")) class(x) <- c("f5", class(x))
  x
}

is.f5 <- function(x){
  inherits(x, "f5")
}

`+.f5` <- function(e1, e2){
  NextMethod(e1, e2)
}

print.f5 <- function(x, ...){
  # Next line from ?GLDEX::digitsBase
  b2ch <- function(db) noquote(gsub("^0+(.{1,})$"," \1", 
                           apply(db, 2, paste, collapse = "")))

  cat("Base 5:\n")
  cat(b2ch(digitsBase(x, 5)))
  invisible(x)
}


x <- as.f5(0:10)
y <- as.f5(5)

x + y

Base 5:
10 11 12 13 14 20 21 22 23 24 30
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文