将代码包装在模块中

时间:2012-02-11 19:08:01

标签: wolfram-mathematica

以下代码无缝执行

wave = HaarWavelet[];
type = "PrimalLowpass";

h = WaveletFilterCoefficients[
  wave, type, 
  WorkingPrecision -> \[Infinity]
];

h = Flatten[
    Take[h, 
        Range[1, Length[h]], 
        Table[2, {Length[h]}]
        ]
    ]

但是当用Module包裹它时,有些错误。考虑函数

    getWaveletFilter[wave_, type_]:=
    Module[{filter}, (* treated as local *)
      filter = WaveletFilterCoefficients[
        wave, type, WorkingPrecision -> \[Infinity]
      ]

      filter = Flatten[
          Take[filter, 
              Range[1, Length[filter]], 
              Table[2, {Length[filter]}]
              ]
          ]

      filter
    ]

我想getWaveletFilter[HaarWavelet[], "PrimalHighpass"] 返回

{1/2, -1/2}
相反,Mathematica有各种各样的抱怨。有什么建议吗?

1 个答案:

答案 0 :(得分:6)

您需要在模块中的每个操作之间放置一个分号以将它们分开:

getWaveletFilter[wave_, type_]:=
Module[{filter}, (* treated as local *)
  filter = WaveletFilterCoefficients[
    wave, type, WorkingPrecision -> \[Infinity]
  ];

  filter = Flatten[
      Take[filter, 
          Range[1, Length[filter]], 
          Table[2, {Length[filter]}]
          ]
      ];

  filter
]

阅读CompoundExpression (;)以便更好地理解。