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

learn more… | top users | synonyms

-4
votes
0answers
37 views

Conditionals within For Loops [on hold]

The following construct: For[i = 1, i <= Length[a], i++, a[[i]]? If[b[[i]] == a[[i]], p1++, p2++]: If[b[[i]] == a[[i]], p3++, p4++]]; yields the ...
0
votes
0answers
23 views
0
votes
1answer
36 views
1
vote
1answer
53 views

Conditional probability syntax [closed]

What syntax do I use to compute this in Mathematica? Game of Yahtzee: 1. Suppose you get three 5’s on the first roll. What is the probability of rolling the other two dice and completing the Yahtzee ...
3
votes
2answers
67 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 ...
14
votes
3answers
426 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 ...
1
vote
1answer
89 views

Manipulating Some Lists in Compile

Consider a list of lists, such as: Z={{1,4,3},{2,1,3},{3,1,4}} Z is an $n \times n$ matrix (here $3 \times 3$) which ...
3
votes
3answers
64 views

How-to select an entry from a list of pairs that meets a condition depending the 2nd element of each pair

Processed data structure data = Transpose[{RandomInteger[{1, 20}, 100], RandomReal[{10^-8, 10^-1}, 100]}] You can download the actual raw data structure here. ...
1
vote
0answers
45 views

recursion and if-statements

So im trying to write a recursion module in mathematica (specifically for strassen multiplication but this doesn't matter). My issue is I don't really know how if statements work in mathematica and if ...
0
votes
2answers
60 views

If Which Piecewise-defined function [closed]

How would I write a function using If and/or Which command for following Piecewise-defined function then plot the function ...
5
votes
0answers
91 views

Find regions in which the roots of a third degree polynomial are real

I have to find the roots of a third degree polynomial in $\phi$ that depends from 3 parameters, namely $t,s,w\in \mathbb R$. In order to do that I've used the command ...
0
votes
1answer
41 views

If/Then Statement Evaluation [closed]

Really rudimentary question here...why is the following if/then statement always evaluating both "True" and "False" expressions and updating my CurCost and Temp variable values? ...
0
votes
0answers
37 views

Impose a condition on a function in a loop

I am trying to calculate something in the following code and I need to omit the final values which are going to be infinity.obviously I want to extract the answers of H2[s] which are zero. ...
0
votes
1answer
48 views

if-conditional tracing a half-sphere [closed]

I have a 2D discrete plane (x,y) and I would like to assign an amplitude to each point in the plane. Each dimension runs from -1 to 1. Say I want to assign an ...
2
votes
1answer
58 views

Change some elements at the second level of a list based on some conditions

I need to change some elements at the second level of a list based on some conditions. I have the following list of objects of the same type: ...
1
vote
1answer
82 views

Fall through multiple cases in switch to execute same function

In C++ / C#, one can fall through multiple cases to execute one function instead of calling that function after each case. For example, ...
10
votes
1answer
231 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 ...
1
vote
1answer
60 views

How to make Which not connect its parts? [closed]

I've noticed that when "Mod" and "Which" are used to the same effect, they plot to slightly different results. Take the following example: ...
1
vote
1answer
48 views

Triangular inequality condition

I have to calculate the following table Table[ ThreeJSymbol[{j1, 0}, {j2, 0}, {j3, 0}], {j1, 1, j1max}, {j2, 1, j2max}, {j3, 1, j3max} ]; in such a way that ...
8
votes
1answer
137 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 ...
1
vote
2answers
53 views

Creating a Table with a list of If statements given in a certain order

I am trying to create a conditional Table with If statements, where I move to the next condition only if the first one is satisfied, otherwise I "Break[]" and move to the next element of the table. ...
0
votes
1answer
70 views
2
votes
3answers
134 views

Join nested lists based on first and last elements within each list

I have a nested list like this: test1 = {{1, 3}, {1, 4}, {1, 7}, {2, 1}, {3, 5}, {4, 1}, {5, 2}, {6, 1}, {7, 6}} I want to join the lists together depending on ...
3
votes
1answer
126 views

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

Let's consider the following: ...
0
votes
1answer
53 views

If statement for evaluating small values

I have a simple calculation which I am trying to use if condition to evaluate summation of some values, here's a code; ...
3
votes
2answers
77 views

An conditional Sum of elements in Array [duplicate]

Consider we have: list = {{2, 3}, {3, 30}, {3, 2}, {4, 53}} and we want to compare the first elements and sum the second elements if first elements are similar ...
3
votes
1answer
117 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 ...
1
vote
1answer
587 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? ...
0
votes
1answer
94 views

FullSimplify not giving me a physically useful answer for Mott scattering

I am using Mathematica to find the cross-section for Mott scattering over an extended target. I evaluated ...
3
votes
2answers
81 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 ...
4
votes
1answer
124 views

Query Dataset to check if row contains any from a set of values

For a given keyless dataset, I can query to return the rows whose columns contain a specific value. For example: ...
4
votes
2answers
122 views

How to conditionally combine a RegionPlot3D with a ContourPlot3D inside a Manipulate?

I know how to combine a RegionPlot3D and a ContourPlot3D, which I did (see below for details on the code I'm referring to) with ...
2
votes
1answer
76 views

Applying Switch and Which to mixed-Key Association lists

Given an Association list w/ a common Key but also some distinct ones: ...
-2
votes
3answers
70 views

Two unrelated questions: 1. Output of Series[] for function, 2. Output conditional answers as if not conditional [closed]

As mentioned in the title, I have two questions. 1. I have code that looks similar to: ...
1
vote
1answer
193 views

Solution of a system of differential equations with a correction factor varying over time

Dear Mathematica users, I am trying to solve a system of equations of first order and time depedent. The system has 5 equations, one for every variable which varies over time. At the moment I am ...
0
votes
1answer
193 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, ...
1
vote
1answer
1k views

Add assumptions to Solve command

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

How to properly use conditionals inside NDSolve

Currently I am working with Mathematica to learn the program, and I'm confused what exactly is wrong with this code. ...
1
vote
1answer
112 views

Solving equations bounded by a region

Straight to the point I have a function ...
2
votes
1answer
67 views

Is there a shorthand for Optional with Condition?

I have a recursive function that does some work to an object for a repeatedamount of times. ...
2
votes
2answers
141 views

RSolve with conditional equation

I have the following conditional equation: $f(n) = 1+2 f(n/2)$ when n is even $f(n) = 1+f((n-1)/2)+f((n+1)/2)$ when n is odd I want to represent this as a conditional equation to be used in ...
3
votes
0answers
115 views

Define (conditional) substitution rule for sum/difference of radicals

I want to tell Mathematica to simplify some expressions applying the following substitutions, conditioned on the fact that $a\geq 0,b \geq 0, a^2-b\geq0 $ $$ \sqrt{a+\sqrt{a^2-b}} \pm ...
1
vote
1answer
87 views

Replacing Logic operator List and generate combination of variables

so I'm trying to do the following starting from a list of inequalities: ...
1
vote
2answers
108 views

Conditional Convolution

I was hoping to incorporate an If function into a Convolve operation, as in ...
1
vote
2answers
182 views

How to select elements from a list by condition?

Considering I have a list: F = {f1, f2, f3, f4} I want to perform exponent on the list F = Power[#, 2]& /@ F Now I ...
3
votes
3answers
168 views

Inefficient NIntegrate and Which

In version 10 a simple numerical integraton of piecewise function is highly inefficient: ...
1
vote
1answer
113 views

Minimize under constraint on an interval

I want find the minima of a (multivariable) function under a constraint which has to be fulfilled on a whole interval, let's say $$ \nabla f (\underline x) = 0 \ \\ \ c(\underline x,s)\geq0\ \forall ...
4
votes
1answer
169 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. ...
1
vote
2answers
193 views

Problems with Solve when constraining solutions to a range

I have the following code ...