Mathematica协助:使用/.用IF []条件替换方法 [英] Assistance with Mathematica: using /. replace method with IF[] conditional

查看:426
本文介绍了Mathematica协助:使用/.用IF []条件替换方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我声明我是Mathematica的新手,这个问题可能很容易回答,但是到目前为止,对于这个困扰Internet的特定问题,我仍然找不到任何帮助.在这里,我基本上总结了我需要执行的代码.在IF条件语句中使用/.替换命令存在一些问题.基本上,我有一个很长的函数,其中包含另一个由IF条件全局定义的函数.这几行代码演示了我遇到的错误...

Let me first state that I am a Mathematica novice and this question is probably easily answered, but I have so far not been able to find any help for this specific problem scouring the internet. Here, I've basically summarized what I need my code to do. There is some problem using the /. replace command with the IF conditional statement. Basically, I have a very long function which contains another function which is defined globally by an IF conditional. These few lines of code demonstrate the error I am running into...

In[1]:= y[x_, z_] = 2*x + 3*z;

In[2]:= zz[x_, z_] := If[y[x, z] < 0, 4*y[x, z], y[x, z]]

In[3]:= zz[-1, -2]

Out[3]= -32

但是我需要...

In[4]:= zz[x_, z_] /. x -> -1 /. z -> -2

Out[4]= If[3 Pattern[-2, _] + 2 Pattern[-1, _] < 0, 
 4 y[Pattern[-1, _], Pattern[-2, _]], 
 y[Pattern[-1, _], Pattern[-2, _]]]

,它不产生预期的数字项.预先感谢大家的帮助,尽管这个问题听起来很愚蠢.注意:我必须使用replace命令,而不是直接将值分配给xz.

which does not yield the expected numeric term. Thanks in advance to all of you for your help, despite how silly this question may sound. Note: I must use the replace command as opposed to directly assigning a value to x and z.

附录:

我简化了我的例子.举个例子:

I simplified my example too much. Take this example:

In[91]:= a[b_, c_] = -3*b + 2*c + d + e + f;

In[92]:= g[b_, c_] := If[a[b, c] < 0, -3*a[b, c], a[b, c]];

In[10]:= g[2, 4] /. d -> 1 /. e -> 2 /. f -> 3

Out[10]= 2 + d + e + f

但是我希望看到结果Out[10]= 8

希望另一个简单的解决方法.

Hopefully another easy fix.

推荐答案

定义函数时,使用x_,它告诉mathematica查找名为x的模式并将其应用到该模式.因此f[x_]=x^2采用表达式 x 并将其转换为 Power[x,2]".

When you define a function, you use x_, which tells mathematica to find the pattern named x and apply the definition to it. So f[x_]=x^2 says "take the expression x and transform it as Power[x,2]".

调用函数时,不再使用模式,而是使用mathematica最终替代x的变量或值.因此,如果未定义a,则f[a]将为您提供a^2,或者为a使用定义并相应地给出结果.

When you call a function, you don't use a pattern anymore, but rather use a variable or a value that mathematica eventually substitutes for x. So f[a] will give you a^2 if a is undefined, or use the definition for a and give the result accordingly.

因此,如霍华德(Howard)在评论中所述,如果仅删除下划线,您将获得所需的表达式.

So as Howard mentioned in the comment, if you simply remove the underscores, you'll get the expression you want.

In[1]:= zz[x, z] /. x -> -1 /. z -> -2

Out[1]= -32

编辑

要回答您的评论,

If仅在条件求值后才对第二个和第三个参数求值.因此,如果您查看g[2,4],您会得到

If evaluates the second and third arguments only after the condition is evaluated. So if you look at g[2,4], you get

If[2 + d + e + f < 0, -3 a[2, 4], a[2, 4]]

尽管a[2,4]本身为您提供了2 + d + e + f,但您仍然看到"then"和"else"语句未得到评估.

you see that "then" and "else" statements remain unevaluated, although a[2,4] by itself gives you 2 + d + e + f.

因此,当您进行替换时,mathematica将开始替换d,e,f在表达式中找到的位置(仅在条件中),一旦它评估了条件并到达"then"或"else"步骤,没有什么可以替代的了.

So, when you do the replacements, mathematica begins replacing d,e,f where ever it finds in the expression (which is only in the condition) and once it evaluates the condition and reaches the "then" or "else" steps, there is nothing else to replace.

您需要使用的是ReplaceAll//.,它们会重复应用规则,直到不再适用.这是

What you need to use is ReplaceAll or //. which repeatedly applies the rules until it can no longer be applied. Here's how

In[2]:= rules = {d -> 1, e -> 2, f -> 3};
        g[2, 4] //. rules
Out[2]= 8

这篇关于Mathematica协助:使用/.用IF []条件替换方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆