Tag Info

New answers tagged

4

This is possibly a duplicate, but the answers given in most cases seem probably more in depth than the OP is looking for, being a newcomer to Mathematica. First you should define f as a function. f[e_] := 7.523190091795795`*^-18 (1.329223358440088`*^17 - 3.9876700753202693`*^18 e + Sqrt[-8.944600854152669`*^34 + 1.4311361366644287`*^37 e^2]); ...


2

So I managed to get what I wanted, thanks a lot for your replies, I am pretty new to Mathematica and they helped a great deal. Here is my code: r3 = r1 r4 = r2 T = 0.01 T1 = T T2 = T T3 = T T4 = T GainD[r1_, r2_, r3_, r4_] := (r4*(r1 + r2) + r2*(r3 + r4))/(2*r1*(r3 + r4)) GainDiv[r1_, r2_, r3_, r4_, T1_, T2_, T3_, T4_] := (((((r4/(2 r1 (r3 + r4)) - ((r1 ...


2

I've cleaned up your code a bit and although I kept your variable names you should be warned that it is never a good idea to start your variables with an uppercase character as this may conflict with built-in names (that always start with a capital). r3 = r1; r4 = r2; T = 0.05; T1 = T; T2 = T; T3 = T; T4 = T; GainD[r1_, r2_, r3_, r4_] := (r4*(r1 + r2) + ...


0

After trying a little bit more, I was able to produce a working version of my compiled function: FC = Compile[{{part1, _Integer, 1}, {part2, _Integer, 1}, {a, _Integer}}, Module[{i1, j1, i2, j2, nnslist}, i1 = part1[[1]]; j1 = part1[[2]]; i2 = part2[[1]]; j2 = part2[[2]]; nnslist = nns[i1, j1]; p[[i1, j1, a]]*Sum[p[[i2, j2, c]], ...


1

A For-loop is a poor choice for your calculation in Mathematica. Better, because it's simpler and faster, is sets = Table[Complement[Range[10], {i, i + 1}], {i, 9}]; which has the additional advantage that the results are available for further calculations. To get the results printed out nicely, use Column Column @ sets


3

You For syntax is just wrong. Try r = Range[10] For[i = 1, i <= 9, i++, k = Complement[r, {i, i + 1}]; Print[k]] and avoid capital letters for your symbol names. K has a build-in meaning Information[K] K is a default generic name for a summation index in a symbolic sum.


1

Map[(f[# - 1] := #) &, Range[3]]; f[n_] := f[n] = f[n-1] + f[n-2] + f[n-3]; Or alternatively: (f[#-1]:= #)&/@Range[3]; f[n_] := f[n] = f[n-1] + f[n-2] + f[n-3]; Just in one line: f[n_] := If[MemberQ[Range[0, 2], n], n + 1, f[n - 1] + f[n - 2] + f[n - 3]];


6

Some version of the following might be useful: ClearAll[f]; Evaluate@Thread[f[{0, 1, 2}]] := {1, 2, 3}; In this case you could also use Set instead of SetDelayed (:=), because the arguments are "atomic", not patterns. Both = and := hold their first argument unevaluated by default, so that a construct like Thread as I am using it above will only work in ...


4

Put this code after your merged cell and evaluate, it should print below all expressions separately. I don't know how solid it is but worth a try: Composition[ Scan[CellPrint[Cell[#, "Input"]] &, #] &, Thread, DeleteCases[#, "\[IndentingNewLine]" | "\n", {2}] &, First, NotebookRead ]@PreviousCell[]


0

Here are two alternative solutions. One involves the use of the Derivative[] operator: f[t_] := Sin[t] Exp[t^2] Plot[Table[Derivative[n][f][x], {n, 1, 4}], {x, 0, 1}, Evaluated -> True] while the other uses the built-in function for the "scaled" derivative: Plot[Table[n! SeriesCoefficient[f[\[FormalX]], {\[FormalX], x, n}], {n, 1, 4}], {x, ...


1

You are running afoul of the (beneficial) scoping that is applied inside Manipulate constructs by way of DynamicModule (or the low-level equivalent). If you "inject" the expression containing g[1] etc. into the Manipulate before it is evaluated it should work correctly I believe: With[{body = gplot[1] == 0}, Manipulate[ ContourPlot3D[body, {x, -1, 1}, ...


4

I have moved the large addendum from my answer to How to program a F::argx message? to this post as I believe it is a better fit here. Please see that link for basic information before continuing. Handling multiple messages with an auxiliary function For full control of Message generation while retaining the canonical behavior of returning an unmatched ...


6

I found by trial and error that Extension-> Sqrt[I] does the job. ExpToTrig[Apart[Factor[1/(1 + x^4), Extension -> Sqrt[I]]]] $$\frac{\frac{1}{4}+\frac{i}{4}}{\sqrt{2} \left(-x+\frac{1+i}{\sqrt{2}}\right)}+\frac{\frac{1}{4}+\frac{i}{4}}{\sqrt{2} \left(x+\frac{1+i}{\sqrt{2}}\right)}-\frac{\frac{1}{4}-\frac{i}{4}}{\sqrt{2} ...


2

There are probably many ways to do this and here are just some examples to get you going into the right direction. First note that you might simply use Sequence: f[a_,b_,m_] := {a :> m[[1,1]]*a + m[[2,1]]*b, b :> m[[1,2]]*a + m[[2,2]]*b} v = {a , b}; m = {{p,q},{r,s}}; f @@ { Sequence @@ v, m } produces (* {a :> {{p, q}, {r, s}}[[1, 1]] a + ...


2

Instead of naively trying Join[v,m] or Catenate[{v,m}], instead you must add an extra level to the matrix argument, using either: Join[v,{m}] (* or *) Catenate[{v,{m}}] which both produce the result {a, b, {{p, q}, {r, s}}} such that you can now use f @@ Join[v,{m}] to produce the desired output {a :> a {{p, q}, {r, s}}[[1, 1]] + b {{p, q}, {r, ...


3

This question is probably a duplicate of: Using pure functions in Table If not I think you want FoldList: FoldList[f, x, Table[If[m < 3, m, m + 1], {m, 5}]] {x, f[x, 1], f[f[x, 1], 2], f[f[f[x, 1], 2], 4], f[f[f[f[x, 1], 2], 4], 5], f[f[f[f[f[x, 1], 2], 4], 5], 6]} As stated I think this is a duplicate but there may be methods applicable here ...


4

One way is to use With ComposeList[ Table[ With[{m = m}, Which[m < 3, f[#, m] &, m < 6, f[#, m + 1] &]], {m, 1, 5, 1}], x] {x, f[x, 1], f[f[x, 1], 2], f[f[f[x, 1], 2], 4], f[f[f[f[x, 1], 2], 4], 5], f[f[f[f[f[x, 1], 2], 4], 5], 6]}


0

Use Evaluate res1 = ComposeList[ Table[ Evaluate[Which[ m < 3, f[#, m], m < 6, f[#, m + 1]]] &, {m, 5}], x] {x, f[x, 1], f[f[x, 1], 2], f[f[f[x, 1], 2], 4], f[f[f[f[x, 1], 2], 4], 5], f[f[f[f[f[x, 1], 2], 4], 5], 6]} res2 = ComposeList[ Table[ Evaluate[Piecewise[{ {f[#, m], m < 3}, {f[#, m + ...


1

These are the transitions I usually use: (both are $C^{\infty}$) f[x_] = Piecewise[{{(Erf[Sqrt[2 π] ArcTanh[x]] + 1)/2, -1 < x < 1}}, UnitStep[x]] g[x_] = Piecewise[{{(Tanh[Sqrt[2] Tan[π/2 x]] + 1)/2, -1 < x < 1}}, UnitStep[x]] Plot[{UnitStep[x], f[x], g[x]}, {x, -2, 2}]


0

In version 10 we can use the new shorthand notation for Composition to write Through@(Identity + Minus@*DiagonalMatrix@*Diagonal)[m] (* {{0, 2}, {3, 0}} *) As noted in the comments, Through@(a - b)[x] won't work the way we'd like because a - b is represented internally as a + (-1)*b. Composing with Minus gets around this problem.


6

This functionality is undocumented, but evident when Spelunking: Rescale[x, {DirectedInfinity[-1], DirectedInfinity[1]}] (* (-2 + x + Sqrt[4 + x^2])/(2 x) *) Rescale[x, {-Infinity, Infinity}] is equivalent to Rescale[x, {-Infinity, Infinity}, {0, 1}]. The relevant entry in the definition when spelunking: Needs["Spelunk`"] Spelunk[Rescale] This returns ...


1

Your Which version might be improved. In general built-in functions do not return $Failed, but themselves, so, to do it, you usually need to use Conditional expressions. This might be written in many more forms, for example, reworking a little your own answer: func::arrayerr = "`1` or `2` must be a valid 3D-array."; func::badpara = "`1` must be a valid ...


3

I believe this is fast enough p[x_] = Integrate[Exp[-y^2 + 2*I*y*x], {y, 0, Infinity}] Zf[x_] := 2 I p[x] f1[x_, p1_] := I*Im[Zf[x]] + p1*I*(1 + Zf[x]); f2[x_, p1_, p2_] := p1*(f1[x] + (2 + Zf[x])/Conjugate[Zf[x]]) - I*p2*(f1[x] - (2 - Zf[x])/Conjugate[Zf[x]]); Plot[Im[f1[x, 2]], {x, -2, 2}] Plot[Im[f2[x, 1.35, 1.45]], {x, -2, 2}] ...


3

Another way to go might be: ClearAll[func] func::arrayerr="`1` or `2` must be a valid 3D-array."; func::badpara="`1` must be a valid real number in the interval (0,1]."; func[_,_,c_] := Message[func::badpara,c] /; c<=0||c>1 ; func[a_,b_,_] := Message[func::arrayerr,a,b] /; !MatchQ[Dimensions[b],{_,3}] && !MatchQ[Dimensions[b],{_,3}]; ...


11

It turns out ListSurfacePlot3D does a terribly poor job of approximating the surface in the OP, otherwise one will just apply DiscretizeGraphics to the output obtained from ListSurfacePlot3D and be done with it. But since that's not applicable here, we present an approach that uses alpha shapes to approximate the shape of the given point set by tuning a ...


2

The definions of warning information func::argnums = "The func called `1` arguments, 3 arguments are needed."; func::arrayerr = "`1` or `2` must be a valid 3D-array."; func::badpara = "`1` must be a valid real number in the interval (0,1]."; Implementations Using the Which to check the style of arguments step-by-step. func[args___] := With[{len = ...


2

You can use replacement rules. For instance, a b + c d + e f /. {a__Plus :> List @@ a, x_ z_ :> {x z}} (*{a b, c d, e f}*) a b /. {a__Plus :> List @@ a, x_ z_ :> {x z}} (*{a b}*) This takes advantage of how ReplaceAll works in terms of the order of the list of replacement rules. It applies the first, and then applies the second to expressions ...


1

eq = (-52271 - 200 x + 100 x^2 - 1058 y + 529 y^2)/52900 == 0; yy[x_] := y /. Solve[eq, y] Plot[yy[x], {x, -25, 25}, PlotStyle -> {Red, Green}, Evaluated -> True] In this case, the same can be done for example with yy[x_] := y /. {ToRules@Reduce[eq, y]} You may get the ellipse span for example with: Resolve[Exists[#, eq], Reals] & /@ {x, y} ...


3

Yet another way: f[x_Integer, y_Integer] := x*10^IntegerLength[y] + y f[68, 54] 6854 Or to generalize: g = Fold[#1*10^IntegerLength[#2] + #2 &, {##}] &; g[89, 68, 54] 896854


4

In light of the update to OP, here's an approach by way of a mathematical formula. (* f[x_Integer, y_Integer] := x*Power[10, Ceiling[Log[10, y]]] + y *) update corrected function should be as follows: f[x_Integer, y_Integer] := x*Power[10, Floor[Log[10, y]] + 1] + y Now let's make some fake data (two sets of 10^5 64 bit integers): xlist = ...


12

fF[x__Integer] := FromDigits[Join @@ IntegerDigits @ {x}] fF[1, 2] (* 12 *) fF[2, 4, 65] (* 2465 *)


2

I was trying to do that with logs and exps but then it dawned on me: f[x_, y_] := ToExpression[StringJoin[ToString[x], ToString[y]]]


2

Updated version To be more in line with what the OP wanted, we have the following updated code. Given inputs (in order) g, totargs = 5, args = {x[1],x[4]}, and targets = {1,4}, if we call the function partialEvaluate[func_, totargs_ args_, targets_] := Block[{num = 1} , Evaluate[func @@ Table[ If[MemberQ[targets, i], args[[i]], Slot[num++]] , {i, ...


1

Since mathematica does not use currying evaluation like language like haskell, you should do the currying for partial evaluation by yourself. Here is some simple example: In[1]:= addx[x_] := Function[y, x + y] In[2]:= addx[3][4] Out[2]:= 7 In[3]:= add3 = addx[3] Out[3]:= Function[y$, 3 + y$] In[4]:= add3[4] Out[4]:= 7 In general, you should make the ...


3

Initially I had simply wanted to tidy up your question and correct your code to make it easier to understand what you are asking for. However, in so doing, I ended up essentially solving the problem, since (as I understood it) there was not much to it after all. Therefore, I take the rather unconventional approach of restating your question in a corrected ...


5

It is often not necessary to use If to check arguments. Rather, since the formal arguments that appear in function definitions are almost always patterns to be matched, you can take advantage of Mathematica powerful pattern matching capabilities. Here is a fairly simple example. validColor = (_RGBColor | _GrayLevel | _Hue); colorToRGB::badarg = "bad ...


2

I'm pretty sure there ought to be something cleaner. While we wait for a better answer, you may use this to return the minimum and maximum number of arguments allowed for each wavelet: nArgs[fun_] := StringCases[ToString@DownValues@fun, Shortest["ArgumentCountQ"~~__~~(n1:NumberString)~~__~~ (n2:NumberString)] :> ...


4

the main issue is that you cannot (readily*) modify the actual argument to a function Try this: InsertRows[vectors_List, matrix0_List,position_Integer] := Module[{matrix}, matrix = matrix0; Do[matrix = Insert[matrix, vectors[[i]], position], {i, Length@vectors}]; matrix] usage: matrix = InsertRows[{vector1, vector2}, matrix, ...


4

Here are a couple of approaches without using a loop, but utilising Flatten and FlattenAt: FlattenAt[Insert[matrix, {vector1, vector2}, 2], 2] {{3, 4, 5}, {10, 11, 12}, {20, 21, 22}, {6, 8, 10}, {9, 12, 15}} ir[vecs_, matrix_, pos_] := Flatten[{matrix[[1 ;; pos - 1]], vecs, matrix[[pos ;; -1]]}, 1] ir[{vector1, vector2}, matrix, 2] {{3, 4, 5}, ...


1

gaps[f_] := Not[Reduce[ Exists[{y}, Element[x, Reals] && y == f, Element[y, Reals]]]] gaps[fuu[x]] // FullSimplify (*x == 0*)


0

Using: SeedRandom[10]; pts = RandomReal[10, {10, 2}]; v = VoronoiMesh[pts]; then f[{x_, y_}] := First@Nearest[pts, {x, y}] /. Thread[pts -> {6, 10, 5, 4, 1, 2, 8, 7, 9, 3}]; re[{x_, y_}] := With[{mp = MeshPrimitives[v, 2]}, Pick[mp, RegionMember[#, {x, y}] & /@ mp]]; Manipulate[ Column[{Show[v, Graphics[{Point[p], Red, ...


4

There are a couple of things wrong in your code: first you need to define the recursions consistently... here I've put all the t-terms on the left and t-1 on the right hand side. Next, you need to specify initial conditions (there weren't any for the fx and fy and the y was only defined implicitly (as 1-x). So here is syntactically correct code: d = 2; k = ...


3

Some parentheses and RuleDelayed instead of a Rule: dot[add[a, b, c], d, e, f] /. dot[add[y__], z__] :> (dot[#, z] & /@ add[y]) (* add[dot[a, d, e, f], dot[b, d, e, f], dot[c, d, e, f]] *) The :> prevents dot[#, z] & /@ add[y] from evaluating until after y and z have been replaced by the expressions they matched. DownValues shows the ...


2

Others have shown you ways to do what you want in ways which are more common to Mathematica. But I think what you really ask for (though it might not necessarily the best solution for your problem) is "pass by reference" which as such does not exist in Mathematica. But there is the possibility to use Attributes for functions and using e.g. HoldFirst will ...


3

Wouldn't it be easier to use Part? a = {{2, 3}, {4, 5}}; a[[1, 2]] = -a[[1, 2]]; a {{2, -3}, {4, 5}}


2

You can use ReplacePart data = RandomReal[{}, {2, 2}] data = ReplacePart[data, {1, 2} -> -data[[1, 2]]] You need to reassign the result back to data You can make the above into a function if needed. flip[data_, i_, j_] := ReplacePart[data, {i, j} -> -data[[i, j]]]; data = flip[data, 1, 2] In your function, you also did something wrong. ...


2

I shall use UnitBox roughly in place of your simplefunction for the sake of example. I shall leave out data but that doesn't really affect the methods that I would use. So we start with: foo[limit_Integer] := Sum[UnitBox[i/20], {i, -limit, limit}] FixedPoint is not directly written to handle this case. Attempting to do this without introducing ...


1

Mathematica cannot evaluate an expression with an undefined variable as (Not)Equal to 0; however, it can evaluate the expression as (Not)SameQ to 0 {2 - x^3 + x^7 == 0, 2 - x^3 + x^7 != 0, 2 - x^3 + x^7 === 0, ! (2 - x^3 + x^7 === 0)} {2 - x^3 + x^7 == 0, 2 - x^3 + x^7 != 0, False, True} fuu[x_] = 2 - x^3 + x^7; Table[D[fuu[x], {x, n}], {n, 0, ...


1

Nearest We can construct such a function $f$ using the definition of Voronoi diagram. Given a set of generating points $p_1, p_2, \ldots, p_n$ the Voronoi cell $R_i$ consists of all points in the plane that are closer to $p_i$ than to any of the other generators. In other words, any point belongs to the Voronoi cell of the nearest $p_i$. Therefore, as ...


1

Let's generate some random points and their corresponding Voronoi mesh diagram: SeedRandom[10] points = RandomReal[10, {10, 2}]; Here are the points and the mesh: Show[ VoronoiMesh[points], ListPlot[points, PlotStyle -> {Red, PointSize[0.02]}], Axes -> False, Frame -> True ] As you mentioned in your question, the Mesh object generated by ...



Top 50 recent answers are included