我需要找到一种使用 C-shell 完成以下任务的方法(我不能使用其他 shell):
有一个程序可以使用伽罗瓦域计算从较大的多项式输出多项式因子。输出是一行,看起来像这样(不要注意数字的实际值;我随机选择它们,它们在数学上不成立):
(0 1 4 6 7 8 11 12 13) = (0 1)^3 * (0 4 5) * (0 2 4 6)^4 * (0 2 3)^2
多项式数学的工作方式是,如果一个因子被提升为偶数,则该因子对于多项式的值来说是多余的。有点像乘以 1。我需要做的是提取多项式因子并消除多余的因子。
使用 sed,我已经能够将上面的表达式更改为
(0 1) ^ 3 * (0 4 5) * (0 2 4 6) ^ 4 * (0 2 3) ^ 2
但我不确定如何进行。
我想将上面的内容输入到 C-shell 脚本中,并进行以下数组赋值:
Array A = (0 1)
Array B = (0 4 5)
我认为最好的方法是首先将多项式因子分成单独的行,如下所示:
(0 1) ^ 3
(0 4 5)
(0 2 4 6) ^ 4
(0 2 3) ^ 2
但我不确定该怎么做。
任何人都可以提供任何有用的帮助或提示吗?请注意,多项式因子的数量会发生变化,但我预计永远不会超过 8。指数值并不重要;我只需要确定它们是偶数还是奇数。如果将它们分配给变量或数组,我可以轻松地做到这一点。单个因素的最大可能大小大概是括号内的 50 个个体数字。
最佳答案
你可能想要像下一个这样的东西:
#!/bin/tcsh
set str="(0 1) ^ 3 * (0 4 5) * (0 2 4 6) ^ 4 * (0 2 3) ^ 2"
echo "My string: ===$str==="
set arrnum = 0
foreach line ( "`echo '$str' | grep -oP '\([^)]*\)'`" )
@ arrnum++
set eval_line = "set array$arrnum = $line"
eval "$eval_line"
end
echo "created $arrnum arrays (array1 .. array$arrnum)"
foreach i (`seq $arrnum`)
set arrname = "array$i"
echo "The content of $arrname"
set temp = `eval echo '$'$arrname`
foreach item ( $temp )
echo $item
end
end
它为每个 (x x x x x)
组创建 array1 .. arrayN。
对于您的字符串:
(0 1) ^ 3 * (0 4 5) * (0 2 4 6) ^ 4 * (0 2 3) ^ 2
打印
My string: ===(0 1) ^ 3 * (0 4 5) * (0 2 4 6) ^ 4 * (0 2 3) ^ 2===
created 4 arrays (array1 .. array4)
The content of array1
0
1
The content of array2
0
4
5
The content of array3
0
2
4
6
The content of array4
0
2
3
关于arrays - C-shell : How to create multiple arrays from a single line of standard input?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24978036/