string manipulation – How can I select rows that contain a specific character in one column and are empty in another?

I’m trying to select rows of a dataset which contain a specific character (d or D) in column #Product (string) and no or missing values in column #Price (formatted as currency). I tried the following:

Select(ds, 
 StringContainsQ(#Product, "d", IgnoreCase -> True) == True && #Price == "" &)

Sadly, I only get an error message. What am I doing wrong?

Thanks for your help 🙂

list manipulation – select partially matching filenames

First, join the two groups:

Join(files1, files2)

Then gather them by their last 3 numbers like:

StringCases("DT120_blur7_Defoc=050um_AstigX=000um_AstigY=260um.png", DigitCharacter ..)

(*Out: {"120", "7", "050", "000", "260"} *)


StringCases("DT120_blur7_Defoc=050um_AstigX=000um_AstigY=260um.png", DigitCharacter ..)((-3;;))

(*Out: {"050", "000", "260"} *)


GatherBy(Join(files1, files2), StringCases(#, DigitCharacter ..)((-3 ;;)) &)

Then select groups that have two elements:

Select(GatherBy(Join(files1, files2), StringCases(#, DigitCharacter ..)((-3 ;;)) &), Length(#) == 2 &)

(*Out: {{"DTideal_circ_def=000um_astX=000um_astY=410um.png", 
         "DT120_blur7_Defoc=000um_AstigX=000um_AstigY=410um.png"}, 
        {"DTideal_circ_def=050um_defX=000um_defY=010um.png", 
         "DT120_blur7_Defoc=050um_AstigX=000um_AstigY=010um.png"}} *)

list manipulation – How to judge whether two groups of sequences are equal in cycles?

There’s a set of arrays that I want to remove repeated elements that are equal after rotation:

arr = {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 1}, {5, 1, 2, 3, 4}, {4, 3, 2, 5,1}};

The elements {1, 2, 3, 4, 5}, {2, 3, 4, 5, 1}and {5, 1, 2, 3, 4}are the same after operation RotateLeft. I want to delete the duplicate elements and only get {1, 2, 3, 4, 5} and {4, 3, 2, 5, 1}.

DeleteDuplicates[arr, RotateLeft[#1] == #2 &]

However, the above operation can only delete the elements that are equal after one shift.

expression manipulation – Combining parametric equations

Thanks for contributing an answer to Mathematica Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

list manipulation – How to create a set (of matrices) which will be used as an finite group to minimize a function?

I’m new here so I’m a little lost.

I need to minimize a function considering that the minimizing parameter belongs to a preestablished set. It all involves matrices.It’s something like this:

enter image description here

Where G is the set I need to create and enter image description here represents an element of the set. All the other parameters are given.

expression manipulation – Unpacking list of arguments into parameters

I have a list in the following form:

list = {{a,1,3},{b,1,3},{c,1,3}}

Now I want to apply this list as parameters for the Sum function so that I end up with the following:

Sum(f(a,b,c),{a,1,3},{b,1,3},{c,1,3})

But I don’t know how to unpack the list like that. “Flatten” just gives a completely flat list.
Am I doing something here which you aren’t even supposed to do?
What would be the most elegant way to do this?

In python for example I would just do this

Sum(f,*list)

list manipulation – Trouble Iterating My Simulated Forest Fire

I’m having some trouble iterating my simulation of a forest fire. I’m new to mathematica, so apologies for poor coding. I’m sure there are far neater and more efficient ways of doing this, but i would like to know whats the problem with the method im using currently if possible.

This is my code to get the adjacent elements, including ‘wrap around’:

neighbourPos(m_, i_, j_) := Transpose(Mod({i, j} + {{-1, 0, 1, 0}, {0, -1, 0, 1}}, Dimensions(m),));

For the fire, 2s represent burning trees, 1s trees and 0s empty ground. After one timestep, trees adjacent to a fire will set on fire and original fire(s) will burn themselves out and become ‘0s’. This my code to simulate the spread of the fire:

nextState(m_) :=  
 (firepos = Position(m, 2); 
  a = MapAt(2 # &, m, 
    neighbourPos(m, Position(m, 2)((1, 1)), Position(m, 2)((1, 2)))); 
  MapAt(0 &, a, firepos))

It seems to work. Up to a point anyway. I would like to eventually plot the thing with ListAnimate, however, When i try to iterate nextState until the fire is burnt out with FixedPointList it stops working properly after 6 iterations and i get a load of errors.

e.g

MatrixPlot(mat/2, PlotRange -> {0, 1}, ColorFunction -> "Rainbow", ColorFunctionScaling -> False)

Gives

Starting matrix

Then

MatrixPlot(nextState(mat)/2, PlotRange -> {0, 1}, ColorFunction -> "Rainbow",  ColorFunctionScaling -> False)

Matrix after one iteration

But then errors come after more iterations.

In(1):= fireStates(m_) := FixedPointList(nextState, m);

In(14):= frames = fireStates(mat);

During evaluation of In(14):= Part::partw: Part 1 of {} does not exist.

During evaluation of In(14):= Part::partw: Part 1 of {} does not exist.

During evaluation of In(14):= MapAt::pkspec1: The expression Mod(1+{}((1,1)),10,1) cannot be used as a part specification.

During evaluation of In(14):= Thread::tdlen: Objects of unequal length in Mod({{0,1,2,1},{1,0,1,2}},{3},1) cannot be combined.

During evaluation of In(14):= MapAt::psl: Position specification Transpose(Mod({{0,1,2,1},{1,0,1,2}},{3},1)) in MapAt(2 #1&,MapAt(2 #1&,<<1>>,{{Mod(-1+{}((1,1)),10,1),Mod({}((1,2)),10,1)},{<<1>>,Mod(<<1>>)},<<1>>,{Mod({}((1,1)),10,1),Mod(1+{}((1,2)),10,1)}}),Transpose(Mod({{0,1,2,1},{1,0,1,2}},{3},1))) is not a machine-sized integer or a list of machine-sized integers.

During evaluation of In(14):= MapAt::partw: Part {3,4,2,1,2,3} of MapAt(2 #1&,MapAt(2 #1&,<<1>>,{{Mod(-1+{}((1,1)),10,1),Mod({}((1,2)),10,1)},{<<1>>,Mod(<<1>>)},<<1>>,{Mod({}((1,1)),10,1),Mod(1+{}((1,2)),10,1)}}),Transpose(Mod({{0,1,2,1},{1,0,1,2}},{3},1))) does not exist.

During evaluation of In(14):= Thread::tdlen: Objects of unequal length in Mod({{1,2,3,2},{1,0,1,2}},{3},1) cannot be combined.

During evaluation of In(14):= MapAt::psl: Position specification Transpose(Mod({{1,2,3,2},{1,0,1,2}},{3},1)) in MapAt(2 #1&,MapAt(0&,MapAt(2 #1&,MapAt(<<1>>),Transpose(Mod(<<1>>))),{{1,1,1},{3,1,2,1,3},{3,2,2,1,2,3},{3,3,2,1,3},{3,4,2,1,2,3}}),Transpose(Mod({{1,2,3,2},{1,0,1,2}},{3},1))) is not a machine-sized integer or a list of machine-sized integers.

During evaluation of In(14):= MapAt::partw: Part {2,2,3,4,2,1,2,3} of MapAt(2 #1&,MapAt(0&,MapAt(2 #1&,MapAt(<<1>>),Transpose(Mod(<<1>>))),{{1,1,1},{3,1,2,1,3},{3,2,2,1,2,3},{3,3,2,1,3},{3,4,2,1,2,3}}),Transpose(Mod({{1,2,3,2},{1,0,1,2}},{3},1))) does not exist.

During evaluation of In(14):= Thread::tdlen: Objects of unequal length in Mod({{1,2,3,2},{1,0,1,2}},{3},1) cannot be combined.

During evaluation of In(14):= General::stop: Further output of Thread::tdlen will be suppressed during this calculation.

During evaluation of In(14):= MapAt::psl: Position specification Transpose(Mod({{1,2,3,2},{1,0,1,2}},{3},1)) in MapAt(2 #1&,MapAt(0&,MapAt(<<1>>),{{2,1,1,1},{2,2,1,1,1},{2,2,3,1,2,1,3},{2,2,3,2,2,1,2,3},<<3>>,{2,3,1,1,2,4},{3,2,3},{3,3,2},<<5>>}),Transpose(Mod({{1,2,3,2},{1,0,1,2}},{3},1))) is not a machine-sized integer or a list of machine-sized integers.

During evaluation of In(14):= General::stop: Further output of MapAt::psl will be suppressed during this calculation.

During evaluation of In(14):= MapAt::partw: Part {2,2,2,2,3,4,2,1,2,3} of MapAt(2 #1&,MapAt(0&,MapAt(<<1>>),{{2,1,1,1},{2,2,1,1,1},{2,2,3,1,2,1,3},{2,2,3,2,2,1,2,3},<<3>>,{2,3,1,1,2,4},{3,2,3},{3,3,2},<<5>>}),Transpose(Mod({{1,2,3,2},{1,0,1,2}},{3},1))) does not exist.

During evaluation of In(14):= General::stop: Further output of MapAt::partw will be suppressed during this calculation.

Many many thanks in advance for anyone kind enough to help.

Cheers.

image manipulation – HDR smartphone cameras: is it done in hardware or software?

Since many smart phone “HDR apps” don’t actually do any real HDR so it’s useful to define what we are talking about.

HDR stands for “high dynamic range”, it’s designed for situations where the highlights are too bright and the dark areas are too dark so the camera can’t capture the entire range of brightness in a single shot.

HDR is done in two steps:

  1. In the first step the software creates an image that does have the entire brightness range, usually by combining multiple shots at different brightness levels – the result from this step is usually an image that looks bland and boring if viewed directly.

  2. In the second step the software takes the result from the first step and process it to enhance details, colors and local contrast – the produces the “HDR look” of images with saturated colors and details everywhere.

Many HDR apps only perform the second step, they take a single non-HDR image and process it to look HDR-ish, you can recognize them easily because they take just one image – for those apps you need a good camera that is capable of capturing the entire dynamic range of the scene (there are no “good” apps in this category since this is all cheating but you do want the better apps because the not-very-best in the category tend to push the saturation and processing way too far into “clown puke” territory and behind).

The real HDR apps take multiple shots – traditionally at least 3 – and combine them, if you have one of those the app is more important because as the entire HDR technique was invented to overcome camera limitations – so it works pretty well with limited cameras.

Obviously the camera does matter, you can get by with a bad camera and a good app but you’ll get better results from a good camera with a good app.

list manipulation – Solve error in multivariable equation

I have the code that works for one variable equation. However, when I modify it to include two coupled equations, solve gives me the error message which I do not understand. Below is the working code for single variable equation,

 ClearAll("Global`*")
 ClearAll(equation, u, x, t);
 equation = x''(t) == x'(t) - x(t)
 x(0) = 1;
 x'(0) = 2;
 terms = 3
 coef1 = Table(D(equation, {t, i}), {i, 1, terms})
 coef2 = Prepend(coef1, equation)
 coef2 = Solve(coef2, {x''(t), x'''(t), x''''(t), x'''''(t)})((1)) /. Rule :> Equal

and the output is,

$left{x”(t)=x'(t)-x(t),x^{(3)}(t)=-x(t),x^{(4)}(t)=-x'(t),x^{(5)}(t)=x(t)-x'(t)right}$

Modifying the above for two coupled equation the code is,

 ClearAll("Global`*")
 ClearAll(equation, u, x, t);
 equation = {x''(t) == -x(t) + y(t), y''(t) == -y(t) + x(t)}
 x(0) = 1; y(0) = 1;
 x'(0) = 2; y'(0) = 2;
 terms = 3
 coef1 = Table(D(equation, {t, i}), {i, 1, terms})
 coef2 = Prepend(coef1, equation)
 coef2 = Solve(coef2, {{x''(t), y''(t)}, {x'''(t), y'''(t)}, {x''''(t), 
  y''''(t)}, {x'''''(t), y'''''(t)}})((1)) /. Rule :> Equal

and it gives me the following error message (from Solve),

Solve: {x’’(t),y’’(t)}is not a valid variable”

The objective is the same as before, to simplify each successive derivative and express it in terms of x(t) , y(t) and x'(t) , y'(t).

How do I fix the Solve issue – “the invalid variable definition” ?