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 ...
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 ...
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 ...
15
votes
2answers
880 views

Flatten not working

I am trying to modify some code proposed here Part assignment is not a symbol and I cannot manage to make Flatten work. I write ...
15
votes
0answers
148 views

Reduce[Sin[x]==0,x] splitting odd and even multiples of Pi

as from Title, I'm wondering why Mathematica does not automatically give a single, simple solution for the problem Reduce[Sin[x] == 0, x] for which I obtain the ...
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, ...
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: ...
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 ...
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: ...
10
votes
5answers
914 views

ListPlot under conditions

I have a data file containing several columns and thousands lines of data. Below I present a small portion of it, in order to express my issue: ...
10
votes
1answer
419 views

Question about Rules and Pattern (The operator patt/;test)

Maybe this question is so trivial but it has confused me. I'm studying the What the @#%^&*?! do all those funny signs mean? and in the Rules and patterns under ...
9
votes
2answers
544 views

Finding the condition for root of a third degree polynomial

I've a third degree polynomial (in $s$): $$as^3+bs^2+cs+d\tag1$$ I need to find the roots of the polynomial, so I can use the code: ...
9
votes
1answer
790 views

Switch inside Manipulate controls statement

I am using this code inside Manipulate: ...
9
votes
2answers
321 views

using memoization with conditional statement

I would like some advice about the use of memoization in conjunction with conditional statements. Let me try to explain my problem: I am constructing a function depending on a number of parameters. ...
9
votes
1answer
135 views

JoinAcross datasets based on Equal and not on SameQ

JoinAcross will join keys if values are the same (SameQ==True) but not if there are equal. For example these two associations are not joined : ...
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 ...
8
votes
2answers
298 views

Weird behavior of conditions when using OptionsPattern and OptionValue

I'm trying to define a function that is evaluated differently depending on its options. This code ...
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
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? ...
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 ...
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 ...
7
votes
5answers
1k views

How can I name patterns in a Switch expression?

Suppose I have an expression and want to match it against successive patterns and return an expression corresponding to the first pattern that matched. In that case, I can use ...
7
votes
5answers
380 views

An anonymous function with constraints [duplicate]

I'm not very good at working with anonymous functions, a fact that has now come back to bite me. Suppose I have a function defined as follows: ...
7
votes
1answer
162 views

Checking what a function depends on

I'd like to define specific partial derivative operators that change based on if the passed expression is a function of, for example, x vs. ...
7
votes
2answers
255 views

Randomly generated coordinates

I want to code that will do the following: Write a Mathematica function called randomHop that takes as its argument a list containing two coordinates x and y; ...
7
votes
1answer
163 views

NExpectation not up to expectations with Boole or Conditioned

Context I am interested in computing numerically the number of extrema at a given threshold for random fields. These numbers are expectations of MultinormalDistributions. Problem This integral ...
7
votes
0answers
258 views

Is it possible to do probabilistic programming by variable elimination with plain Mathematica 10 or later?

Using frameworks like Figaro it is possible to do probabilistic programming by variable elimination to build a probabilistic model and then run inferences against it in a very intuitive way. Example: ...
6
votes
4answers
246 views

Take elements from a list based on two criteria

I am trying to take elements from a list of tuples using 2 criteria. An example of the list is: list={{-1, -1}, {-1, 1}, {1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}} ...
6
votes
3answers
183 views

How to create a sublist of elements with a congruence relation

I generated a list made up of lists of four elements such as: n = {{49, 9, 9, 5}, {57, 13, 9, 5}, {81, 21, 13, 5}, {95, 19, 19, 7},...} I would like to create a ...
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. ...
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: ...
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
975 views

Flow/graph-based programming in Mathematica?

Is it possible in Mathematica to write a program inside a graph? For example if you have a bunch of different If[] statements that you need to embed within one ...
6
votes
1answer
103 views

Conditional rendering of items

Let's say I have a column to display and each of its items can be hidden separately. Question: Is there a way to make it work without re-rendering whole column whenever a particular item is hidden? ...
6
votes
0answers
893 views

When are `If`, `Piecewise`, `Switch`, and `Which` interchangeable and when are they not?

Experimenting with joint dependent distributions via the TransformedDistribution function, I used the following to derive a distribution where the second variate is ...
5
votes
3answers
554 views

Arrange a list in ascending order by deleting list elements

I am new to mathematica and currently learning it by solving some basic problems. I am trying to arrange a list in ascending order using a detailed IF loop. For instance if we start out with $\{1,65,...
5
votes
3answers
329 views

How to kick out points that are specific to a function

I have a list of xi,yi points. I want to remove any points from the list where x^2+y^2<1. I probably have to write a for loop of some sorts, not sure how to begin. ...
5
votes
4answers
676 views

List, map function based on a condition

I have the following list: ...
5
votes
2answers
670 views

How to optimize the following function?

Consider the following function: function[x_, y_] = If[x < Exp[y], Sqrt[1 - x/Exp[y]], 0]; functionTotal1[x_] = Sum[function[x, 0.02*j], {j, 0, 4*10^5, 1}]; ...
5
votes
3answers
169 views

Among the first 10,000 multiples of 17

Hello I'm new with Mathematica and I can't find a way where I can do this condition. Among the first 10,000 multiples of 17 how many have the sum of their digit multiple of 17? The part for the ...
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
2answers
8k views

Nested If statements

When I do this: Apply[(If[#2 == what, "yay", If[#2 == whoops, "nay"]]) &, {-5, what}] I get: yay But when I do this: <...
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 ...
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
5answers
180 views

How to plot zeros of a function as a thin stripe?

I have this function for $0<x<20$ f[x_] := Cos[x] + 2 Cos[2 x]; and I want to graphically represent the points where this function is zero, as a thin line ...
5
votes
2answers
159 views

conditional expectation doesn't work with SmoothKernelDistribution

I'm trying to calculate a conditional expectation E[ x | x > a ], where the distribution of x is inferred from a finite ...
5
votes
2answers
294 views

There is no ConditionalExpression warning for an Indeterminate case in Integers

This expression yields Pi in the limit m -> n in the Complexes domain. But for ...
5
votes
2answers
277 views

Better use of pattern matching in replacing elements of a list

I have a list of reals and I'm replacing those which are contained in a given interval with two different values depending on wheter the selected reals are positive or negative. I came up with this ...
5
votes
1answer
105 views

An unexpected match when using Condition, Optional and passing a Sequence

Mathematica is behaving in an expected way when passing a parameter with an Optional and a Sequence. Here is an example: ...

1
2 3 4 5
9