Wolfram mathematica 在Mathematica中定义自定义符号
我经常需要提取以将值列表限制在子列表中,即如果Wolfram mathematica 在Mathematica中定义自定义符号,wolfram-mathematica,Wolfram Mathematica,我经常需要提取以将值列表限制在子列表中,即如果vals给出vars={x1,x2,x3,x4},我需要svars={x2,x4}的值,我需要限制[list,vars,svars]其中 restrict[vars_, svars_, vals_] := Extract[vals, Flatten[Position[vars, #] & /@ svars, 1]] 我想通过为restrict[vars,svar,vals] (来源:) 我的问题是 实现这一点的好方法是什么 这完全是个
vals
给出vars={x1,x2,x3,x4}
,我需要svars={x2,x4}
的值,我需要限制[list,vars,svars]
其中
restrict[vars_, svars_, vals_] :=
Extract[vals, Flatten[Position[vars, #] & /@ svars, 1]]
我想通过为restrict[vars,svar,vals]
(来源:) 我的问题是
好的符号可能非常有用-但我不确定是否需要这个特殊的符号 这就是说,
符号
包使这变得非常简单。由于使用符号调色板时有许多隐藏框,因此我将使用一个屏幕截图:
通过使用Action->PrintNotationRules
选项,您可以看到底层的NotationMake*
downvalues构造。在[4]中,在屏幕截图中生成
NotationMakeExpression[
SubscriptBox[vals_, RowBox[{vars_, "|", svars_}]], StandardForm] :=
MakeExpression[
RowBox[{"restrict", "[", RowBox[{vars, ",", svars, ",", vals}],
"]"}], StandardForm]
NotationMakeBoxes[Subscript[vals_, vars_ | svars_], StandardForm] :=
SubscriptBox[MakeBoxes[vals, StandardForm],
RowBox[{Parenthesize[vars, StandardForm, Alternatives], "|",
Parenthesize[svars, StandardForm, Alternatives]}]]
关于2:我将传递规则列表
线程[vars->vals]
,而不是单独跟踪名称和值。我最喜欢的Mathematica习惯用法之一是将规则列表与
with rules
一起使用,如下所述:此构造对with
块中的表达式求值,其中所有替换符号都已(递归定义)过。这允许你做一些事情,比如
WithRules[{a -> 1, b -> 2 a + 1}, b]
这会让你在命名参数上走得更远
SetAttributes[WithRules, HoldRest]
WithRules[rules_, expr_] := Module[{notSet}, Quiet[
With[{args = Reverse[rules /. Rule[a_, b_] -> notSet[a, b]]},
Fold[With[{#2}, #1] &, expr, args]] /. notSet -> Set,
With::lvw]]
编辑:WithRules
结构基于这两个usenet线程(感谢Simon挖掘它们):
Notation
autoloadnotationpalete==True`当您需要[“Notation]”时,它应该会自动打开。
。如果没有,那么您可以从ToFileName[{$InstallationDirectory,“AddOns”,“Packages”,“Notation”,“localpaletes”,“English”},“]
打开它和/或从调色板菜单安装它。