Questions tagged [conditional]

Questions about programming or mathematical constructs that enable verification, selection, or branching behavior when one or more criteria are determined to be True.

Filter by
Sorted by
Tagged with
59
votes
8answers
24k views

How to avoid returning a Null if there is no "else" condition in an If construct

I need to use If but with only one option that is if "a" then do "b", else do nothing. So I wrote If[a,b] but the problem is ...
12
votes
2answers
3k views

Evaluating an If condition to yield True/False

I would like to decide whether an option passed to my custom function has the value Automatic or something else. This is my attempt: ...
41
votes
6answers
6k views

2D random walk within a bounded area

I want to simulate a random walk in two dimensions within a bounded area, such as a square or a circle. I am thinking of using an If statement to define a boundary. ...
24
votes
3answers
1k views

Quick way to use conditioned patterns when defining multi-argument function?

I have a simple function that is supposed to only accept numeric values (i.e. complex/real numbers and constant symbols e.g. Pi, E). $$f(a,b,c)=a+b+c$$ Edit: I should have chosen a less simple ...
5
votes
1answer
1k views

Simplifying conditional expressions using assumptions does not work

I have a conditional expression f[x_] := x /; x ∈ Reals This works fine for normal numbers and symbols, e.g. ...
0
votes
1answer
327 views

Set conditions at a permutation

I need to find all permutations of 3 elements from {a,b,c,d,e} with this condition: These must include element d maximum once, ...
12
votes
4answers
16k views

How to remove ConditionalExpression from output

I need define the function with parameter so I could directly generate a power series expansion. The problem is that in some cases ConditionalExpression appears in output. Here is my code: ...
8
votes
1answer
20k views

Add assumptions to Solve command

I want Mathematica to solve the following by assuming the part within the Log function, which is ...
3
votes
1answer
255 views

How to use Compile to generate an $n\times n$ array using $n$ vectors

Let's consider the following: ...
3
votes
1answer
462 views

How do I color Riemann integral rectangles using conditionals?

I'm building a function to visualize the Riemann integration process for any function using rectangles. Everything is fine, except that I need the color of the rectangles to change if they're under ...
21
votes
3answers
1k views

Generating Tuples with restrictions

I am interested in a certain subset of all tuples. Generating all with Tuples and then filtering is wasteful, and will "blow up" very quickly. For a concrete ...
8
votes
2answers
3k views

Generate "nice" random matrix

I don't frequent this community, so please give criticism if this is a poor question. I am trying to make a tool which generates a small (say, $4\times 4$) matrix with integer entries, which has a "...
8
votes
6answers
3k views

Simplifying nested If statements

Consider : list = {100,102,103,99,106,107,104,112} I want to consider, extract only the "big moves" of that time series. In this case the moves bigger then 5 ...
6
votes
2answers
4k views

How to restrict the domain of a function

I realize this question has been asked many times, but I am fairly new at Mathematica and the other answers are very complex, and certainly too complicated for this simple code I am trying to write. ...
4
votes
1answer
356 views

Why does pattern test not work in this case to restrict the function parameters?

I was looking at this Q&A about using pattern test (pattern_?test) vs pattern condition (pattern_/;cond) and came across ...
5
votes
2answers
281 views

How to condition arguments of a pure function?

I would like to exclude the point {x=0,y=0} in the function definition ...
5
votes
4answers
309 views

Recursive function, if and which statements

I am given this question: Let a function $ T_n(x) $ be defined by: $$ T_{n+1}(x) = \frac{1}{x} T_{n-1}(x) - \frac{2}{7} T_n(x), $$ where $ T_0(x) = 1, T_1(x) = x $. I need to construct a recursive ...
3
votes
2answers
127 views

Faster use of Condition for a large array (see the example)

Here is the example/problem. I have a array of $n$ random number. n = 2000; p = RandomReal[{0, 9}, n]; I have to create a $n\times n$ sparse matrix with $(i,j)$...
2
votes
3answers
262 views

Join matrix based on conditions

Given the following matrix: ...
0
votes
1answer
219 views

PlotStyle based on If condition

So in the If[evecs==Re[evecs], I want to then change the colour in PlotStyle of the ...
0
votes
1answer
100 views

Create bold headings and rows in `TableForm` if `x` is prime

In the following table ...
0
votes
0answers
37 views

Mathematica Function Taxicab Numbers [duplicate]

I have spent days trying to compile this code and I cannot generate a list of taxicab numbers for the Hardy-Ramanujan Theory. Here is the module I wrote earlier: ...
0
votes
0answers
78 views

3D-plot of a maximization of a definite integral

I'm applying the answer (If method) suggested by 'ThatGravityGuy' in this post in a more complicated case. Consider my objective function (pardon its formidable ...
8
votes
1answer
24k views

Multiple lines in an If statement [closed]

What I am wondering is how do you perform multiple lines when using the If statement. I know the following is not proper Mathematica code, but how would I do this in Mathematica? ...
14
votes
2answers
1k views

Difference between Piecewise, Which, If?

I have the impression that these functions have different evaluation rules. I cannot produce a minimal example right now, but in general, what are the differences between these functions? That is, ...
5
votes
1answer
630 views

How to split list into segments based on first element of sublist, and remove ineligible segments

I have a list that looks like this: ...
8
votes
1answer
485 views

Why does pattern test work only with pure functions?

Why is it that pattern test works only with pure functions? Cases[Range[0, 70], _?(Divisible[#, 7] &)] Cases[Range[0, 70], x_?(Divisible[x, 7])] I ...
6
votes
4answers
512 views

How to simplify block of multiple repetitive If statements?

I'm building a boardgame...game in Mathematica. I have the board state represented as a matrix. It's a two player game, and each player (let's call them A & B) only has one type of piece or '...
6
votes
2answers
646 views

How to select points from a table that are within some region

there, I encountered this problem while coding: I generate a table in the r-z plane of cylindrical coordinates: ...
5
votes
1answer
232 views

Multiple down-values with exactly the same lefthand side

I was working on an answer to this question, when I was sidetracked by what I bring up here. I wanted a function, which when given a string representing a binary number, would convert the string to a ...
5
votes
1answer
667 views

Can one compile the Switch function?

I am aware that one can compile Which, e.g.: ff = Compile[x, Which @@ {x < 0, 2, x >= 0, 3} ff[1.] ...
5
votes
1answer
222 views

Check if a symbolic matrix is hermitian

I have a symbolic matrix: Potential = SparseArray[{Band[{1, 1}] -> V0/2, Band[{2, 1}] -> V0/4, Band[{1, 2}] -> V0/4}, {2*max + 1, 2*max + 1 }] ...
4
votes
5answers
1k views

Creating a conditional table

I'm trying to create a conditional table. Let's say I want to have such result: {1,2,3,4,5,0,0,0,0,0}. The idea is to create a table of n elements (10 in a given example), but when one element takes ...
2
votes
1answer
127 views

Solving Piecewise Differential Equation

I am trying to simulate something using differential equations. However, the equations are piecewise. Here is my code: ...
2
votes
5answers
925 views

How to leave function unevaluated programmatically?

Is it possible to write a function in such manner, that it can be either evaluated or left unevaluated? This is a general question. Sample below is just a sample. No need to find solution for this ...
2
votes
2answers
105 views

Why does conditional NExpectation fail for a multivariate distribution?

To give an example for a multiparametric distribution let us take a binormal distribution: ...
1
vote
1answer
163 views

Modifying a list of matrices with conditional statement

I have a list of around 100 matrices, that looks like this ...
1
vote
3answers
772 views

Find Array Elements That Meet a Condition

I have an array of 5 numbers (say m). I want the following operation m=[-1,-3,-2,-5,-4] For (j=1,j==5,j++) if ...
0
votes
1answer
51 views

Plotting an integral where the integrand depends on the parameter values (`If`)

With my function $f(x)$ I would like to Plot3D $\int_a^b f(x) dx$ with the parameter values $a \in [0,1]$ and $b \in [1,3]$. The challenging part has to do with ...
12
votes
3answers
1k views

Double Sum Involving Condition

I would like to compute the dimensions of some small free nilpotent Lie algebras. However, I am totally new to this and I could not figure out how to write the double sum which gives the dimension of ...
5
votes
2answers
950 views

ContourPlot with certain contour values omitted

Suppose I want contour plot of $x^2-y^2$, for $x,y\in[0,1]$ (my actual function is more complicated). Over this interval, $x^2-y^2$ is sometimes positive and sometimes negative. Using ...
5
votes
2answers
150 views

What is the syntax to conditionally suppress plotting in version 9?

Bug introduced in 9.0 and fixed in 9.0.1 In Mathematica 7 it is very easy to conditionally suppress plotting of individual lines using If: ...
4
votes
1answer
371 views

How to use Assuming and If together?

I would like to compare variables t and u and print either Yes or ...
4
votes
6answers
764 views

How to avoid applying Cross to a single argument?

I am looking for the simplest way to avoid applying Cross to a single argument. ...
3
votes
1answer
282 views

Table doesn't replace inside conditionals (If, Which) [duplicate]

This: Table[If[cond, y], {y, 1, 3}] outputs this: {If[cond, y], If[cond, y], If[cond, y]} The ...
3
votes
2answers
119 views

Except[] with levelspec

I am trying to determine all the variables used in list. For This I use: DeleteDuplicates[Cases[l1,_Symbol,-1]] This is great except that ...
3
votes
2answers
246 views

Delay Evaluation of First Argument of ConditionalExpression until Second Argument Evaluated

A particular set of equations I am solving, sol = Solve[...] yields a lengthy List of ...
2
votes
1answer
41 views

Which condition inside a loop with unknown value in a condition

I have a Which condition inside a loop such as: ...
2
votes
2answers
302 views

How to plot data with different colors (or symbols) depending on a condition [duplicate]

Let's consider a simple example. I want to plot a function on [0;2], depending on a condition: if x<1, I want to plot ...
2
votes
1answer
236 views

Generate conditions seems to not work [closed]

I am trying to compute the following integral Integrate[E^(I*k*Omega*t), {t,0,T}, GenerateConditions->True] for which Mathematica returns ...