Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
1 vote
0 answers
60 views

Is there an easy way to make built-in functions do a replacement before throwing an error message?

Many built-in functions throw errors before executing a ReplaceAll. The functions still execute the ReplaceAll as intended, but ...
crabtree's user avatar
  • 153
0 votes
2 answers
91 views

Same code behaves differently as a pure function and a function, and insider another function

I have a rule-transformer that takes a list of rules (and a list generator = {σ[z], σ[-1], σ[1]}) ...
Lelouch's user avatar
  • 555
7 votes
7 answers
487 views

How do I withdraw the rhs from a list of equations?

I have a list of equations that were solved for different values of another parameter and stored in a Table and has the form: {a==0, a==1, a==2, a==3} and so on. I ...
qubiKat's user avatar
  • 73
5 votes
2 answers
241 views

Do not understand evaluation order of ReplaceAll [duplicate]

{x, x^2, Length@x, Subsets[x]} /. x -> {1, 2, 3} (* {{1, 2, 3}, {1, 4, 9}, 0, {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}} *) with error What ...
lesobrod's user avatar
  • 1,924
4 votes
1 answer
179 views

RuleDelayed (:>) involving Piecewise leads to Piecewise::pairs warning in ReplaceAll (/.)

When I write a replacement rule like this: Piecewise[{{x^2, x < 0}}] /. Piecewise[a_, b___] :> Piecewise[a, b] It works fine, but I get the unexpected ...
JEM_Mosig's user avatar
  • 3,043
4 votes
4 answers
193 views

How to use replacement rules in a named expression before a sub-expression evaluates?

I have a named expression: expr := a + b. a and b are also named expressions. Let's say <...
Borislav Stanimirov's user avatar
2 votes
2 answers
158 views

Replacing symbolic variables inside of a compile

As the result of a symbolic integration, I have a very long array with symbolic variables u[i] which I would like to substitute for real number values. Moreover, I ...
mmen's user avatar
  • 163
1 vote
0 answers
59 views

How to get rid of "/." in my result, when I evaluate a term containing ReplaceAll?

In the previous code I defined Bchoice and pchoice In one line I defined ...
Jakob's user avatar
  • 151
0 votes
0 answers
35 views

Define a new rule to take precedence over the others in a symbol's `Downvalues` list [duplicate]

I have a symbol that has a very large list of Downvalues. One of the rules in this list should be applied before the others because it simplifies many expressions, ...
Diffycue's user avatar
  • 1,854
2 votes
1 answer
360 views

What's the difference between Evaluate and Replace command? [closed]

all, I'm new to Mathematica, I am very confused between Evaluate and Replace (/.) command. I feel they are very similar. For example, ...
VinCC899's user avatar
0 votes
1 answer
73 views

Why does DifferenceRoot evaluate only in this table?

I would like to use a complicated function containing DifferenceRoot as a replacement rule, but somehow it evaluates only in ...
Pxx's user avatar
  • 711
2 votes
1 answer
43 views

Formula: setting parameters vs replacement, different results?

I have a question that may sound stupid and have an easy answer - but I currently do not see it. I have defined a function of four variables, $f[a,b,c,d]=...$, in what I think is correct Mathematica ...
Al Waurora's user avatar
1 vote
1 answer
55 views

The number 1 evaluates inside HoldForm sometimes

If I run the following code: ...
Mattia's user avatar
  • 175
2 votes
0 answers
48 views

How to refactor solution verification function for solving simple ordinary differential equations with Dsolve? [closed]

I have tried different approaches but none would work for me as I am new to both functional programming and Mathematica. At bottom is the code I have. How ...
Jules Manson's user avatar
  • 2,467
1 vote
0 answers
41 views

How to replace expressions in a `HoldComplete` and then evaluate them? [duplicate]

Example: ...
bcegkmqs23's user avatar
6 votes
0 answers
90 views

In over my head in trying to create a debugger

Apologies for a less than ideal question, but if I could have asked a good question the answer would probably have been revealed in the process... In answer to this question @LeonidShifrin gave some -...
Julian Moore's user avatar
  • 2,570
3 votes
1 answer
100 views

Prevent argument substitution in held expression when injecting into unevaluated code

I want to write a custom sow/reap pair to wrap any piece of code in sow[code] and call reap ...
István Zachar's user avatar
2 votes
1 answer
168 views

How to get ReplaceAll to happen before function call

I have a problem I am attempting to solve with NMinimize. I have a scoring function, which will give me a final score. The problem has many variables, so instead of having them as parameters, they ...
Helix888's user avatar
3 votes
1 answer
226 views

ReplaceAll does not work

Why ReplaceAll does not work in this situation? ...
Agnieszka's user avatar
  • 697
0 votes
1 answer
54 views

Constructing sequence of function with elements depending on the previous one / recursion

Starting from a function $\theta0[x\_ , y\_ , z\_ ]$, the terms $\varphi[x\_ , y\_ , z\_ ]$ constitutes a sequence of functions that is constructed as follows: Order 1 (one function and its ...
DanielAmmering's user avatar
5 votes
3 answers
404 views

First ReplaceAll, then evaluate an expression

Here is a simple example using ReplaceAll: rvec[[2]] /. rvec -> {x, y} I think Mathematica first tries to evaluate ...
Alexander Erlich's user avatar
2 votes
0 answers
49 views

Using Min & Max of an Interval in a replacement [closed]

I want to use a replacement rule to transform a list of equations. The following works as expected: ...
Chris K's user avatar
  • 20.4k
3 votes
2 answers
219 views

Convert defined variables to rule list

I'd like to make a function that takes a list of variables and returns a corresponding rule list with the current values of the variables. E.g. ...
Chris K's user avatar
  • 20.4k
10 votes
3 answers
204 views

Perform localized, evaluation-leak free replacements

Consider the following expression: expr = Hold[ { f[ {1, {Print@1}} ], g[ {{{Print@1}}} ] } ]; I'm looking for a way to ...
Lukas Lang's user avatar
  • 34.4k
3 votes
4 answers
401 views

Basic substitution

I am relatively new to Mathematica, so I am sure this is basic, but I lack the basic terminology to be able to google it. How can I make this command work? ...
Tohiko's user avatar
  • 133
6 votes
2 answers
246 views

How to use ReplaceAll and RuleDelayed with an Association? [duplicate]

I expected that something like this ...
user42582's user avatar
  • 4,225
1 vote
1 answer
56 views

Setting the parameters in a defined function vs replacement rule in the formula

I have the following definition in my Mathematica notebook. ...
user avatar
3 votes
0 answers
51 views

How does the code idiom for replacing part of a held expression work? [duplicate]

I've seen this idiom used in different programs but never fully understand why it is necessary. Could someone explain? Is it still the recommended way to replace some part of a held expression to its ...
user13253's user avatar
  • 8,674
1 vote
0 answers
255 views

Problem in evaluating nested anonymous functions [duplicate]

It seems that I have found a bug (or a fundamental problem in the design of the programming language of Mathematica) with functions defined as rules. Define the function g as g(1)=2 and g(2)=1. We can ...
Mauri Ericson Sombowadile's user avatar
2 votes
0 answers
118 views

Avoid evaluating left-hand side of rule

I am trying to force some integrales in the form $\int \mathrm{d}x f(x)+g(x)$ into $\int \mathrm{d}x f(x)+\int \mathrm{d}x g(x)$. My first naive solution was ...
Lorenzo Pistone's user avatar
1 vote
0 answers
51 views

Evaluation of an expression for different functions [closed]

Suppose I have function a[t_]:=f'[t]/f[t]. The actual expression is more complicated but the idea is the same. I want to evaluate ...
Furkan Semih Dündar's user avatar
18 votes
1 answer
405 views

Pattern matching to a function evaluation inside an Association

I find the following behaviour deeply counter-intuitive: <|1 -> 123.456 |> /. {x_?NumericQ -> Round[x]} (* <|1 -> Round[123.456]|> *) That ...
Emilio Pisanty's user avatar
2 votes
2 answers
313 views

Evaluating ReplaceAll inside SetDelayed, first

Consider the following (simplified for the purpose of example) definitions: f[x_] := y /. y -> x g[x_] := x Where for example, the definition of ...
Shwouchk's user avatar
  • 517
5 votes
2 answers
105 views

Why does `Times` evaluate within `Hold` when doing replacement?

Hold @ t[t[1,1],2]/.t-> Times Hold @ t[t[1,2],2]/.t-> Times Hold[(1 1) 2] Hold[2 2] Why doesn't the second one give <...
masterxilo's user avatar
  • 5,779
19 votes
1 answer
940 views

Is this a bug in Replace?

Consider the following minimal examples. This one works as expected, replacing trees starting from the inside with a numerical value. ...
István Zachar's user avatar
2 votes
0 answers
159 views

Using Replacement Rule on Part of List? [closed]

Why can't I use a replacement rule to replace a variable name standing in for the index of a list? For example: myList={1,2,3,4,5} myList[[index]]/.index->3 ...
user13999's user avatar
  • 907
0 votes
1 answer
1k views

Replace argument of delayed function before it evaluates [duplicate]

How should I use replace so that f[x_] := x^2 Replace[f[3], 3 -> 1] OUT = 9 evaluates to 1, instead of 9? This way, the replacement of the argument is done ...
Zlatko-Minev's user avatar
1 vote
2 answers
247 views

How to define a function returning x -> 2 while x = 1?

What I want is an f that will work like this: ...
Lee David Chung Lin's user avatar
11 votes
2 answers
2k views

What is the Difference Between Rule (->) and RuleDelayed (:>)?

I am new to Mathematica so this question may seem rudimentary. Sorry for that! :) I want to implement the well-known property of Kronecker's Delta $$\Sigma_{i=1}^{n} a_i \delta_{ij}=a_j,\quad 1 \le ...
Hosein Rahnama's user avatar
3 votes
0 answers
133 views

Check an Option without Evaluation

Consider the following: ...
Mark Messa's user avatar
0 votes
2 answers
162 views

Unevaluated derivatives of known functions after replacement

I want to make the following replacement work correctly: Array[(2 D[xM[[#1]][t], t] * D[xM[[#2]][t], t]) &, {3, 3}] /. {xM -> {Sin[ω t], Cos[ω t], 0}} ...
lurscher's user avatar
  • 743
17 votes
1 answer
325 views

When is the righthand side of a RuleDelayed evaluated?

The documentation on RuleDelayed states that lhs :> rhs represents a rule that transforms lhs to rhs, evaluating rhs only after the rule is used. Consider the ...
Fred Simons's user avatar
  • 10.2k
1 vote
1 answer
219 views

Replace symbols in output without evaluation

I found the Wolfram example for ExpandNCM but I want a more human-readable output form. I want to evaluate ExpandNCM, for example with ...
pbx's user avatar
  • 842
0 votes
2 answers
110 views

Different behaviour of Map and Inner in replace rules

Why does x[{1, 2}] /. {x[l_] -> Map[func, l]} {1, 2} not behave as expected, while for example ...
Severin's user avatar
  • 63
5 votes
2 answers
145 views

Why does Rule behave like this?

Why does {x, x^2, x^3, a, b} /. x^n_ -> f[n] result in {x, f[2], f[3], a, b} when ...
d125q's user avatar
  • 437
2 votes
1 answer
120 views

Return the incomming expression on false

when I have an expression, that is unknown to mathematica, it is returned exactly the same, as it was called. For example SeeFlyingPigs[1,2] called on the line ...
jmodrak's user avatar
  • 23
3 votes
3 answers
250 views

ReplacePart equivalent for Associations

I have a fairly complicated Association. I wish to use/create a function such as ReplacePart to operate on certain Keys within this multi-level Association. Here's ...
kale's user avatar
  • 11k
5 votes
2 answers
184 views

ReplaceAll performance problem: packed arrays on the LHS are unpacked when the RHS is too long

The following is one of the most mysterious performance problems I encountered and came up while extending the booleval function mentioned here. I am looking for a ...
Szabolcs's user avatar
  • 236k
1 vote
2 answers
120 views

Evaluate differentiation after replacement

The problem is Find two linearly independent solutions of $2x^2y'' -xy' + (-4 x + 1)y = 0, \, x>0$ of the form $y_1 = x^{r_1}(1 + a_1 x + a_2 x^2 + a_3 x^3 + \cdots)$ and $y_2 = x^{r_2}(1 + b_1 x ...
tintin's user avatar
  • 749
1 vote
1 answer
180 views

Commutative algebra by generators and quadratic relations

My apologies if this has been asked before. Please also be gentle, as this is my first question here. I am trying to define a commutative algebra by generators and relations in the following way. ...
Matt Hogancamp's user avatar