# Tag Info

## New answers tagged programming

18

In this case I don't know how to post something helpful without providing full code so I'll just do that and hope this wasn't homework. My emphasis is on clarity (hopefully) rather than brevity or peak efficiency. flip = # /. {LightRed -> LightBlue, LightBlue -> LightRed} &; flipNeighbors[i_, j_] := (color[##] = flip @ color[##];) & @@@ ...

2

If you are interested in increasing performance I would recommend using FindMaximum. If you go through five steps you will discover at the end the original account balance (a0) is related to the five withdrawals via: a0 == (w5 + w4 z + w3 z^2 + w2 z^3 + w1 z^4)/z^4 where w1 through w5 are the withdrawal values and z is the interest rate. It is desired ...

3

Solution based on the GridGraph SeedRandom[10801]; dimension = 20; coDimension = 30; percProbability = 0.7; deleteMe = Pick[Table[i, {i, dimension*coDimension}], Table[RandomReal[] > percProbability, {i, dimension*coDimension}]]; G = GridGraph[{dimension, coDimension}, VertexLabels -> "Name", ImagePadding -> 30]; G = ...

7

Here's a solution using MorphologicalGraph[]: SeedRandom[10801]; dimension = 100; coDimension = 30; percProbability = 0.7; myData = Table[Table[Boole[RandomReal[] < percProbability], {i, dimension}], {j, coDimension}]; img = Binarize@Image@myData; Now all you need to do is use FindShortestPath[]. For example, the shortest path from top-left ...

3

Maybe you want something like this?: eq = 4/2 x (1 - x) (x - 1/2) - 1/2 m x^2 sols = x /. Solve[expr == 0, x] (* Out[2] := {0,1/8 (6-m-Sqrt[4-12 m+m^2]),1/8 (6-m+Sqrt[4-12 m+m^2])} *) realandimaginarysolutions = Through[{Re, Im}[#]] & /@ sols (* Out[3] := {{0,0} ,{1/8 (6+Re[-m-Sqrt[4-12 m+m^2]]),1/8 Im[-m-Sqrt[4-12 m+m^2]]} ,{...

4

You can use my OptionsValidation framework to add options validation to your functions. We start by loading the package: Import["https://raw.githubusercontent.com/jkuczm/MathematicaOptionsValidation/master/NoInstall.m"] Now "register" tests you want to perform on option values. You do it by defining CheckOption for your function. ClearAll[func] Options[...

2

Understanding $Context,$ContextPath the parsing stage and runtime scoping constructs A symbol in Mathematica can never be without a context. We can assume that the internal representation of any symbol stores a string of the form "contextsymbol". But for you as a programmer, there are ways to enter a symbol without stating it's full context: x, Sin, x ...

11

After years of development, I'm releasing a package called MTools on github. The package is under an MIT license. You can fork it and send pull requests. The main contribution of MTools is to allow object oriented programming in Mathematica in a very natural way. The package also contains: Generic classes for manipulating trees of objects and displaying ...

2

Here is a solution that with the help of LibraryLink: CAGDBezierSurface[pts_, u_, v_] := Module[{m, n, row, col}, {m, n} = Dimensions[pts, 2] - 1; row = BezierNonzeroBasis[m, u]; col = BezierNonzeroBasis[n, v]; bezierSurf[row, col, pts] ] pts = Table[{ Cos[2 Pi u/6] Cos[v], Sin[2 Pi u/6] Cos[v], v}, {u, 6}, {v, -1, 1, 0.5}]; f = BezierFunction[...

0

Association has HoldAllComplete attribute Association is not just another head denoting a special kind of list of rules or a certain kind of object. It has some properties that make querying it more efficient, but also cause it to behave differently from a List. You should be aware of this if you intend to use Associations as a data structure for object ...

1

I offer the following based on Monte Carlo integration, but trying to make use of the facilities Mathematica provides: weights = {2/3, 1/3}; means = {-3, 2}; sd = Sqrt[2]; distributions = NormalDistribution[#, sd] & /@ means; fdist = MixtureDistribution[weights, distributions]; qdist1 = TransformedDistribution[Log[PDF[fdist, f]], Distributed[f, ...

2

Here is a solution via the Wolfram LibraryLink technique: First, let us make a comparison between BezierNonzeroBasis[n, u] and BernsteinBasis[n, Range[0, n], u] Do[BezierNonzeroBasis[10, 0.1], {10000}] // AbsoluteTiming Do[BernsteinBasis[10, Range[0, 10], 0.1], {10000}] // AbsoluteTiming BezierNonzeroBasis[10000, 0.1]; // AbsoluteTiming ...

3

Association/<||> objects are Atomic and thus unmatchable before 10.4 AtomQ@Association[] yields True. This is confusing because it is not stated anywhere in the manual. For example tutorial/BasicObjects#15871 claims that only numbers (including complex ones), Strings and Symbols are atomic objects. guide/AtomicElementsOfExpressions does not mention ...

2

In general one should not expect to obtain a general symbolic solution (a function x[a,b]) to the given equation since there are two independent variables, see e.g. Solve symbolically a transcendental trigonometric equation and plot its solutions for certain aspects regarding transcendental equations. To get an idea how the solution depends on parameters a ...

2

Perhaps this! It works if you only need the plot and not the values, or the function. That would require more work. ContourPlot3D[ Evaluate[-x + (Log[f2[x]] - Log[f1[x]] + Log[b] - Log[a])/((1 - a) + (1 - 1/f1[x]) - (1 - b) + (1 - 1/f2[x]))] , {a, 0, 1}, {b, 0, 1}, {x, 0, 1} , AxesLabel -> {"a", "b", "x"} , Contours -> {0} , Mesh -> None ...

Top 50 recent answers are included