Tag Info

Hot answers tagged

34

You can use custom transformation rules, for example: -11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2 //. (a : _ : 1)*s_Symbol^2 + (b : _ : 1)*s_ + rest__ :> a (s + b/(2 a))^2 - b^2/(4 a) + rest returns (* -25 + (-1 + x)^2 + (-2 + y)^2 + (-3 + z)^2 *) The above rule does not account for cases where b is zero, but those are easy to add too, if ...


25

The power of Mathematica's syntax allows us to create a dice in several different ways. Here's one way that I like: dice[n_Integer] := dice[n, Black] Format[dice[n_Integer, c_]] := With[{ dots = {1 -> {5}, 2 -> {3, 7}, 3 -> {3, 5, 7}, 4 -> {1, 3, 7, 9}, 5 -> {1, 3, 5, 7, 9}, 6 -> {1, 2, 3, 7, 8, 9}} /. l : ...


20

I always use IntegerString for this (I also number my files in a similar way): In[1]:= IntegerString[#, 10, 2] & /@ Range[87] Out[1]= {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", \ "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", \ "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", \ "33", "34", "35", ...


19

Edit: Quoting from Heike´s comment: "The font families used for Greek, script, gothic, and double struck symbols are respectively "Mathematica1", "Mathematica5", "Mathematica6", and "Mathematica7" " With this knowledge, just use Styletogether with the FontFamily option: Style["Doth this help?", FontFamily -> "Mathematica6", FontSize -> 100] ...


19

We have unicode support so we can use the following strings: {"⚀", "⚁", "⚂", "⚃", "⚄", "⚅"}: dice = FromCharacterCode /@ Range[9856, 9856 + 5]; Grid[Partition[RandomInteger[{1, 6}, {50, 2}], 5] /. { i : {__Integer} :> Style[ Row[dice[[i]], Spacer[1]], {Large, Total[i] /. {7 -> Red, _ -> Black}}]} , Frame -> All]


17

I've had a need for such a function several times, and I found this implementation of C-style *printf functions, by Vlad Seghete. To use it, all you need to do is extract the files to $UserBaseDirectory/MathPrintF/ and you're all set. Here's an example once you've installed it: <<MathPrintF` sprintf["%d %s %d %s, %s %s %s %s", Sequence @@ ...


17

Since a native method is not forthcoming, I shall post my file based circumvention, for Windows. You will need to have this utility in the command path (it apparently is stock with Windows 7). copyUnicode[expr_] := Run["clip <", Export["$Clipboard.temp", ToString[expr, InputForm], "Text", CharacterEncoding -> "Unicode"] ]; Usage: expr ...


16

Not as such. The closest equivalent is StringForm, but it doesn't provide the formatting options that the printf family does. StringForm gets a lot of use in the creation of messages. Example: StringForm["The value of Pi is ``", NumberForm[N[Pi], 3]] (* ==> "The value of Pi is 3.14" *) Note that StringForm does not create a string, it merely ...


15

Here is a definition for mixedForm that works for all cases, i.e. proper and improper fractions and integers. Clear[mixedForm] mixedForm[Rational[x_, y_]] := If[Abs@x > y, HoldForm[#1 + #2/y], x/y] & @@ (Sign@x QuotientRemainder[Abs@x, y]) mixedForm[x_Integer] := x Some examples: mixedForm /@ {2, 4/5, 10/3, -3/4, -5/2} Out[1]= {2, 4/5, 3 + ...


15

After importing a free dice 3D model {pd, vd} = Import["c:\\dice.stl", #] & /@ {"PolygonData", "VertexData"}; g2 = Translate[GraphicsComplex[vd, Polygon /@ pd], {-10, -37.5, -10}]; rv = {{0, 0, -1}, {0, -1, 0}, {0, 0, 1}, {0, 1, 0}, {-1, 0, 0}, {1, 0, 0}}; dice[x_List, n_Integer] := Rasterize@(Graphics3D[{EdgeForm[None], Blue, Rotate[g2,{{0, 0, ...


14

Another option is to set FormatType -> OutputForm on the $Output stream: SetOptions[ $Output, FormatType -> OutputForm ]; Print["Hello"]; Or call OutputForm on the string itself: Print[ OutputForm["Hello"] ];


14

There is already a built in function to handle this — it's called ScientificForm. You can get the output you desire as: ScientificForm[12345.^6, NumberFormat -> (#1 <> "E" <> #3 &)] // ToString Out[1]= 3.53954E24 StringForm only affects the display, so to export it correctly as a string to a log file, you'll have to wrap it in ToString ...


14

This answer is just a quick hack. I think that to make true extensible character might not be something that an end-user can do... Anyway, redefine the formatting for OverHat using OverHat /: MakeBoxes[OverHat[a_], form_] := With[{s = First[Rasterize[a, "RasterSize"]], ab = MakeBoxes[a]}, With[{sl = N[2 Log[2 s]]}, ...


14

Maybe this : NumberForm[#, 10] &@ {123.189094`, 123.189263`} {123.189094, 123.189263 } ? Edit Consider also this utility of NumberForm[ x, {m, k}] giving m real digits of x with k digits to the right of the decimal point, e.g. NumberForm[#, {10, 7}] &@ { 197.9898987322333, 201.73205080756887 } { 197.9898987, 201.7320508 }


14

PrintPrecision You can control the number of digits displayed using the PrintPrecision option. You have a number of options for its use. You can set it Globally or for the specific Notebook using the Options Inspector. You can also use it directly with Style: Style[123.189094, PrintPrecision -> 10] 123.189094 You can set it temporarily for one ...


14

The answer to your first question is that PlotMarkers doesn't really use a graphics primitive, but uses font based markers as a proxy for it. This can lead to errors in positioning on some OSes. I'm guessing that PlotStyle has something of the form ToString@HoldForm[...] when the input is a list, which is why None and False or anything else get converted to ...


14

If you want to order your terms this way but not perform the other formatting that TraditionalForm does, you might like to try the (undocumented) PolynomialForm[expr, TraditionalOrder -> True]. That will change output like this: Expand[(x+y-1)^3] (* -> -1+3 x-3 x^2+x^3+3 y-6 x y+3 x^2 y-3 y^2+3 x y^2+y^3 *) into this: ...


14

Start by making some similarity measure of sentences, here I use one that takes number of words in common divided by number of words in longest sentence. The measure is then used to connect sentences that are similar enough in a graph and extracts the connected components: strs = {"Barack Obama", "Barack H. Obama", "Barack Hussein Obama", "Obama ...


14

nb2 = NotebookOpen[$InstallationDirectory <> $PathnameSeparator <> "SystemFiles" <> $PathnameSeparator <> "FrontEnd" <> $PathnameSeparator <> "StyleSheets" <> $PathnameSeparator <> "Core.nb"]; Note that some of the named styles in the core stylesheet styles are empty, i.e. the style name is ...


13

An even simpler way that does not require you to figure out the tick positions, is to set the tick font opacity to 0 and the tick font size to 0 to avoid the excess margin where the ticks would have been. Here's an example: RegionPlot[Sin[x y] > 0, {x, -1, 1}, {y, -1, 1}, FrameTicksStyle -> Directive[FontOpacity -> 0, FontSize -> 0]] ...


13

Mathematica does not have a built-in equivalent to sprintf. The closest thing is StringForm which allows placeholders within a string to be replaced, but does not perform any formatting of the replacement values: sprintf[control_, args___] := StringForm[control, args] // ToString sprintf["hello, ``", "Joe"] (* "hello, Joe" *) It is not pretty, but we ...


13

You could mimic the panelled look by doing something like panelBox[pt0 : {x0_, y0_}, pt1 : {x1_, y1_}] := {{GrayLevel[.7], Rectangle[pt0, pt1]}, {White, Polygon[{Offset[{.3, .3}, pt0], Offset[{2, 2}, pt0], Offset[{2, -2}, {x0, y1}], Offset[{-2, -2}, {x1, y1}], Offset[{-.3, -.3}, {x1, y1}], Offset[{.3, -.3}, {x0, y1}]}]}, {GrayLevel[.2], ...


13

This is similar to my Log question and similar methods can be used. $PrePrint = # /. { Csc[z_] :> 1 / Defer@Sin[z], Sec[z_] :> 1 / Defer@Cos[z] } &; Example: (x + y) Csc[x] Sec[y] (x + y)/(Cos[y] Sin[x])


13

<<19>> or Skeleton[19] means that some output (here 19 elements) is omitted. See the ShortAndShallowOutput tutorial in the Documentation Center for more information.


13

You can use PolynomialForm : Collect[(1 + x + Cos[s] x^2)^3, x] // PolynomialForm[#, TraditionalOrder -> True] & Cos[s]^3 x^6 + 3 Cos[s]^2 x^5 + (3 Cos[s]^2 + 3 Cos[s]) x^4 + (6 Cos[s] + 1) x^3 + (3 Cos[s] + 3) x^2 + 3 x + 1


12

You can express any fraction/number to arbitrary decimal places by using a backtick followed by number of digits required. For example: In[1]:= 4/3`20 Out[1]= 1.3333333333333333333 This is the same as N[4/3, 20]. Now combine this with AccountingForm, which never uses scientific notation to get the output that you desire. AccountingForm[1/998001`2994] ...


12

Admittedly hackish, but you could use numbers = RandomReal[10^10, 3] ToString[ToString[#, CForm] & /@ numbers] or ToString[ToString[#, FortranForm] & /@ numbers] It gives {3.672422352722051e9, 8.491123505444411e9, 1.7587409493599138e9} Mathematica usually likes to wrap long lines. This won't happen here because by default ToString uses ...


12

Make a graphics where you put at each position {i,j} the entries of your matrix. The only thing left to do is to transform your path positions from the form {p1,p2,...} into {{p1,p2},{p2,p3},...}, which can easily done with Partition. Then you can map Arrow over this list and have your result m = RandomInteger[{0, 10}, {5, 5}]; path = {{1, 1}, {2, 1}, {3, ...


12

You were definitely on the right track with MonomialList. Here is a solution. Others will probably find nicer ways. Using the trick found here, we first define a Format that looks like "Plus" but doesn't rearrange things: Format[myPlus[expr__]] := Row[Riffle[{expr}, "+"]] With this format in hand, we can wrap your original function in the following: ...


12

In the old days, when "making the Numerator rational" was often wanted, I came up with the following set of rules: EvaluiereAt[pos:(_Integer|{__Integer}),f_:Identity][expr_]:= ReplacePart[expr,pos->Extract[expr,pos,f]]; EvaluiereAt[pos:{{__Integer}..},f_:Identity][expr_] := Fold[ReplacePart[#1, #2 -> Extract[#1, #2, f]] &, expr, ...



Only top voted, non community-wiki answers of a minimum length are eligible