是否有用于查找复数并计算它们的 R 函数?

Is there an R function for finding complex numbers and counting them?

我生成了 1000 个 2x2 矩阵,其元素是介于 -10 和 10 之间的随机数。

我提供了到目前为止的代码。

但我不确定这是否是确定我的特征值列表是否复杂的正确代码。然后对于每个矩阵,我必须确定系统是否是一个稳定的节点(特征值都是实数和负数);一个不稳定的节点(两个特征值都是实数和正数);鞍形(两个特征值都是实数,一个为正另一个为负);稳定的焦点(具有负实部的复特征值);不稳定的焦点(具有正实部的复特征值);或中心(虚部特征值,实部为零)。

我也设置了计数器,但不确定如何合并它们。当我输入代码时,没有任何显示。

M=lapply(1:1000, function(z) matrix(runif(1000,min=-10,max=10), ncol = 2, nrow = 2))

eig=lapply(M, eigen)

V=sapply(eig, `[[`, "values")

SFcounter=0

if (is.complex(V)==T)
Re(V)>0

SFcounter=SFcounter+1

对于每个 node,您都在 V 中创建了一个列。您可以使用 Im 函数提取任何复数的虚部(无论是等于零还是其他)。您可以使用 Re 提取实际组件。此外,如果你对实数分类感兴趣,那就是所有满足 Im(V) == 0 [虚部等于零] 的那些。

如果您使用 apply,您可以评估 V 中的每个特征值对,它们在每一列分组在一起。根据您不同的分类标准,您可以使用 if 语句来识别这些点:

node.classification <- function(x) {
  if ( (Im(x) == 0) && (Re(x) < 0) ) { #both eigenvalues are real and negative
    return("stable")
  } else {
    if ( (Im(x) == 0) && (Re(x) > 0) ) { #both eigenvalues are real and positive
      return("unstable")
    } else {
      if ( (Im(x) == 0) && sum((Re(x) > 0) == 1) ){ #both real one pos./one neg.
        return("saddle")
      } else {
        if ( (Im(x) != 0) && (Re(x) < 0) ) { #each complex, real part negative
          return("stable focus")
        } else {
          if ( (Im(x) != 0) && (Re(x) > 0) ) { #each complex, real part positive
            return("unstable focus")
          } else {
            return(NA)
          }
        }
      }
    }
  }
}

head(apply(V, 2, node.classification))
[1] "unstable"       "stable focus"   "stable"        
[4] "unstable"       "unstable"       "unstable focus"