New answers tagged

0 votes

Replacement of a variable only for a part of an expression

You may either use ReplacePart (close to what @Alrubaie proposed) expr = a^2 + f[a]; ReplacePart[expr, 2 -> f[c]] (* a^2 + f[c] *) or map the replacement ...
user avatar
0 votes

Replacement of a variable only for a part of an expression

You just need ReplacePart Where ReplacePart[Function/Expression , Location which part to replace] ...
user avatar
  • 1,018
2 votes

How to replace anti diagonal elements of a matrix

Making use of the method posted by ciao (in an answer to How to zero (or replace) the diagonal of a square matrix?) ...
user avatar
  • 10.5k
3 votes

How to replace anti diagonal elements of a matrix

idm = IdentityMatrix[Length@m] (res = Reverse /@ (m idm) + m (-(Reverse /@ idm ) + 1) ) // MatrixForm $$\left( \begin{array}{ccccc} 100 & 9 & 3 & 8 &...
user avatar
  • 16.2k
3 votes

How to replace anti diagonal elements of a matrix

use element-wise operation. keep the diag-matrix and reverse it. anti = mat // Diagonal // (*take diag*) DiagonalMatrix // (*make mat*) Map[Reverse] (*rev it*) {...
user avatar
  • 6,817
4 votes

How to replace anti diagonal elements of a matrix

We can subtract the anti-diagonal to remove it and then add the Reverse of diagonal. ...
user avatar
  • 32.7k
7 votes

How to replace anti diagonal elements of a matrix

From The Procedural Dodo: If you can do it in place (destroying the orginal m), this will be fast: ...
user avatar
  • 216k
2 votes

How to replace anti diagonal elements of a matrix

With[ {mask = Reverse[ReplaceAll[DiagonalMatrix[Function /@ Diagonal[m]], 0 -> Identity], 2]}, MapThread[Construct, {mask, m}, 2]]
user avatar
  • 5,224
5 votes

How to replace anti diagonal elements of a matrix

Using ReplacePart: ...
user avatar
5 votes
Accepted

How to replace anti diagonal elements of a matrix

This is the kind of question that might trigger the "how-many-ways-can-you-do-it" game. I'll kick it off with this: ...
user avatar
  • 5,224
2 votes

'Mapping' the values of a list to variable

Different way: expr1 = a[n] + b[n] stencil = {-1, 0, 1} f[ex_, sten_] := Sum[Map[# + tp &, ex, {2}] /. tp -> sten[[i]], {i, 1, Length[sten]}] Then, ...
user avatar
  • 890
6 votes
Accepted

Replacing a trigonometric value

See Why doesn't Mathematica know the exact value of some trivial trigonometric functions? FullSimplify[FunctionExpand[Tan[42 \[Degree]]]] ...
user avatar
  • 216k
2 votes

Replacing a trigonometric value

expression /. Tan[42 Degree] -> (-Sqrt[6 - 2 Sqrt[5]] + Sqrt[6 (5 + Sqrt[5])])/(2 Sqrt[ 7 - Sqrt[5] + Sqrt[30 - 6 Sqrt[5]]])
user avatar
  • 3,782
6 votes

Constructing a RootSum object changes existing internal syntax

Another workaround is not to put the single Function variable in a list. ...
user avatar
5 votes
Accepted

Timing Discontinuity with Replace[] Function

There are a couple things going on here, from what I can tell. First, my guess is that at some level, somehow, it has something to do with the fact that we suddenly need more bits to represent all the ...
user avatar
  • 8,208
5 votes

Replacing large numbers

Using Condition: l /. x_Integer /; x > 10^6 -> 0 (*{{1, 2, 3}, {4, 0, 5}}*) Or in the same way: ...
user avatar
15 votes

Replacing large numbers

You could use 3-arg Clip: Clip[l, {-Infinity, 10^10}, {0, 0}] {{1, 2, 3}, {4, 0, 5}} where I used ...
user avatar
  • 124k
2 votes

Replacing large numbers

l = {{1, 2, 3}, {4, 10^(16), 5}} Define a high, low threshold as well as a substitute. thlow = 2; thhi = 3; subst = 11; Find ...
user avatar
  • 16.2k
2 votes

Replacing large numbers

I'm assuming you want something generalizable. Let's say your threshold is 10^10, then something like this: Map[If[# > 10^10, 0, #] &, l, {2}] You could ...
user avatar
  • 5,224
6 votes
Accepted

Constructing a RootSum object changes existing internal syntax

The bug does not exist for Slot[1], just for Function with named parameters. (A dummy variable is substituted for ...
user avatar
  • 216k
1 vote

How to use a rule such that it substitutes the value x and derivatives of x in the expression?

Rather than answering directly, let me try to you give some background info that will hopefully help you understand this yourself. Suppose you enter an expression such as ...
user avatar
2 votes

How to use a rule such that it substitutes the value x and derivatives of x in the expression?

The short form of your functions since you don't give the whole expression F = 2*m + H[t, r] + D[H[t, r], r, r] + D[H[t, r], t, r] set the rule to include ...
user avatar
  • 7,419
5 votes
Accepted

How to use a rule such that it substitutes the value x and derivatives of x in the expression?

To obtain H -> Function[{t, r}, G[t, r]]form, use the following: ...
user avatar
13 votes

Simple, fast re-defining a part of 2D list

Another option is to use ReplacePart ...
user avatar
  • 109k
11 votes

Simple, fast re-defining a part of 2D list

...
user avatar
  • 216k
6 votes
Accepted

Can substitution rules be used in function definitions?

It's not a problem with using rules in the def; it's a problem of whether x literally appears in the rhs or not. When applying a definition where the pattern ...
user avatar
  • 8,208
3 votes

MatchQ returns False for a match for e1 ** e2 while matching both e1 and e2

Your seem to want your last expression interpreted as (2 e1) ** (-2 e2), i.e the NonCommutativeMultiply between (2 e1) and ...
user avatar
  • 60.2k
0 votes

Why do two different Mathematica files with the same ODEs substituted with the same values give me different outputs?

The problem was that the numerical data values should be put right before the NDSolve command execution (not earlier)-then it can handle this really well. I dont know why it is such a difference for ...
user avatar
  • 47
3 votes

FindFit with Replace error

To get things to work I changed the following: (1) = to := in the definitions of k1eqn and <...
user avatar
  • 34.1k

Top 50 recent answers are included