Tag Info

New answers tagged

2

Fix your error The initial value of index i should be 1, not 0. If the value is 0, it means extracting the Head of the nk and Z, namely, List . In addition, i < Length[Z] should be i <= Length[Z], Zm2[Z_, nk_] := Module[{zm2 = 0, i}, For[i = 1, i <= Length[Z], i++, zm2 += nk[[i]]*Z[[i]]*Z[[i]];]; Return[zm2]; ] Zm2[z, n] 1590.73 ...


3

Instead of f @ (g @@ x) you can use f @ Apply[g] @ x (since Mathematica 10). This is more verbose, but it saves parentheses. Since visually matching parentheses is hard, I think that the latter variant is more readable, especially when there's a long chain of functions. This style can be used with postfix too (x // Apply[g] // f) and it makes it easy to ...


0

Grid[Join[ Transpose[Union /@ GatherBy[Flatten[lst1], First][[All, All, 1]]], Transpose[Union /@ GatherBy[Flatten[lst1], First][[All, All, 2]]]]] which of course can be streamlined...


4

First problem, subscripts start with 1 in Mathematica, not 0 like in many other languages. One alternative z = List[82, 57, 40, 22, 41, 8]; n = List[0.1973, 0.002, 0.1068, 0.0930, 0.002, 0.5989]; Zm2[Z_, nk_] := Sum[nk[[i]]*Z[[i]]^2, {i, 1, Length[Z]}]; Zm2[z, n] which gives 1590.73 Or perhaps just using Total[n*z^2] which gives the same result, but ...


1

I cannot comment, so that is a suggestion: a = Zm2[z, n][[1]] Thus a is a variable you are looking for. You may aviod the need for this just starting loop from i=1, not i=0. And, depending on your needs, set i < Length[Z] + 1. So, your result if you loop through 5 elements will be 1552.4, but with all 6 elements you will get 1590.73. Note, that ...


1

Supposing that you want a DownValues definition rather than a Function you could use: (* with your expression assigned to expr *) (x \[Function] x_) /@ Variables[expr]; f[Sequence @@ %] = expr;


0

Why not to use a list for your arguments? Something like: f1[αvars_, βvars_] := {αvars[[1]] + ..., ..., βvars[[5]] ... } Or, as I can see you have double indicies for your variables. Can you formulate your equations as matrix manipulation? This can significantly simplify the task if possible.


6

If you have a list of variables stored in vars in the desired order and expr is the function's formula, then f = Function @@ {vars, expr} will define a function for you. If you're content with the variables being in their sorted order and the expression expr is polynomial-ish (test Variables[expr] first), then f = Function @@ {Variables[expr], expr} ...


6

It was actually a lot easier than I thought f = Function @@ {vars, expr}


1

f[ω0_, α00_, α01_, α02_, α10_, α11_, α12_, α20_, α21_, α22_, β01_, β02_,β11_, β12_, β21_, β22_] := ... where ... is the list in your OP. N.B. you're missing ω1 as an argument, I assume that's intentional...


6

Without a more complete example of your function all I can offer are bare guidelines to (hopefully) point you in the right direction. The first level is merely syntactical; you would use OptionsPattern, OptionValue etc., in a high level function simply for convenience, but pass all arguments as machine types to an inner compiled function. A second level is ...


2

I think there is another way to do that: f[s : (PatternSequence[_?NumericQ] ..)] /; Length[{s}] == 3 := Function[{a, b, c}, a^2 Sin[b] Log[c]][s] It's suitable for the cases that conditions are the same.


2

This seems to accomplish what you wanted, but it's not returning your result. I have commented the code a bit more heavily than usual to show how I put it together and what it accomplishes: hopefully this may be helpful to you in constructing similar functions in the future. function[a_, b_] := Module[ {factorlist, listofsolutions, toCR}, (* Generate ...


3

Walk through this a step at a time with each line of input followed by the line of (* output *) a = 17; b = 697; F = FactorInteger[a*(27*b^2 + a^6)] (* {{2, 2}, {13, 1}, {17, 3}, {37, 1}, {67, 1}} *) v = Map[First[#]^Last[#] &, F] (* {4, 13, 4913, 37, 67} *) (* When it returns {ReplaceAll[x],p} there was no solution *) {x/.ToRules[Reduce[{(x+a)^3==-b, ...


0

This is a possible implementation of what I was proposing, to get you started: Plot[ Interpolation[#, InterpolationOrder -> 1][x] & /@ {r1a, r2a}, {x, 0, 9.1}, Evaluated -> True, Epilog -> {{Red, Point[r1a]}, {Blue, Point[r2a]}} ]


1

Yes, for this you can use Map (as operator /@) which is one of the most often used functions when doing the same job for several inputs. For instance creating 3 data sets {g1, g2, g3} = Range /@ {10, 20, 30} and then creating your interpolations {i1, i2, i3} = Interpolation[#, InterpolationOrder -> 1] & /@ {g1, g2, g3} If you don't know what the ...


2

You can still use the old functions from Combinatorica, either to tide you over while you figure out the new ones, or as a semi-permanent solution. WRI has also provided a handy guide to transitioning from that package to the new built-in functions: "Upgrading from Combinatorica" For example, in the case of the obsolete code you mentioned: << ...


1

Code outline: ClearAll[XMLNote]; (*modified, only act on target tags*) XMLNote[ XMLElement[tag : "section" | "TextHeading", attributes_, data_], m_Integer ] := ... (*new, for other tags pass down XML elements*) XMLNote[XMLElement[_, attributes_, data : {__XMLElement}], m_Integer] := Sequence @@ (XMLNote[#1, m + 30] &) /@ data; ...


4

Preamble There are some important differences (or, more precisely, features of Function which can't be reproduced with symbols and rules), that have not been reflected in answers here, but that I think deserve a separate answer. These are related to some more advanced uses, involving evaluation control, closures, and garbage collection. Emulating Hold ...


5

Let's start with slightly modified version of mergeRules, that takes into account fact that options can have symbolic or string names and name -> val is treated the same as "name" -> val: ClearAll[symbolToName, deleteOptionDuplicates] symbolToName[sym_Symbol] := SymbolName[sym] symbolToName[arg_] := arg deleteOptionDuplicates[opts:OptionsPattern[]] ...


9

Following your clarification this seems to be OK, though I would agree that a cleaner solution would be nice: Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[ FilterRules[{opts, Options @ f}, Options @ #] & /@ {Button, Tooltip}, OptionValue[foo] ] // Column Test: f[1, 2, ...


2

Sorry for being unprecise. I'm just wondering if your solution is the "official" way to deal with optional list arguments like in ImageTake. I also tried f[args:{x_, y_}:{1,2}] := {x, y}which seems to work. However, I'm not sure if understand it the right way... I do not believe I have seen a guideline or even a standard practice for this particular ...


2

The question deals with a particular operator characterized by the commutation relation $[a, a^\dagger] = 1$. The polynomial to be expanded is also of a special form, $(a+a^\dagger)^n$. Also, the whole polynomial is supposed to be applied to the unique state vector $\vert 0\rangle$ that has the property $a \vert 0\rangle = 0$ (which is the null vector). So ...


6

There are many ways to achieve this, I'd probably do something simple like f[] = f[{}] (*set or set delayed, depends of context*) But you can also do something more fancy, although it is not compact enough for me to like it :P Default[f] = {}; f[Optional@{x_: 1, y_: 2}] := {x, y} f[] f[{}] {1, 2} {1, 2}


0

BoundaryDiscretizeGraphics was a very helpful function. I was able to take it from there. The following code achieved what I wanted: boundaryPoints={{0, 0}, {4, 4}, {8, 1}, {12, 0}, {8, -1}, {4, -4}}; bsc = BSplineFunction[boundaryPoints,SplineClosed->True]; reg = BoundaryDiscretizeGraphics @ ParametricPlot[bsc[t],{t,0,1}]; regBdry = ...


7

reg = BoundaryDiscretizeGraphics@ ParametricPlot[ BSplineFunction[{{0, 0}, {1, 0}, {2, .5}, {1, 1}, {0, 1}}, SplineClosed -> True][t], {t, 0, 1}] Plot3D[Sin[6 x y], {x, y} ∈ reg]



Top 50 recent answers are included