Tag Info

Hot answers tagged

27

Leonid provides a nice method for doing this within "pure functions" but I think it should be pointed out that the common method for doing this is pattern matching. I argue that destructuring is the foundational use of pattern matching in Mathematica. Every replacement pattern, be it an explicit rule (:>, ->) or part of a definition (:=, =), that ...


20

Here is my attempt to generate a pseudocode for algorithmicx package. Current features Functions (as SetDelayed) Set If with 2 and 3 arguments While Do (any number of variables of any type) New! Return Module, With, and Block CompoundExpression (;) Test functions (EvenQ, etc., and Not@EvenQ, etc.) New! Indentation Code ClearAll[pseudocode, pscd] ...


14

You can use macros / code generation to be able to use the syntax you like. Here is one possibility: ClearAll[withLiteralIndices]; SetAttributes[withLiteralIndices, HoldAll]; withLiteralIndices[code_, inds : {__Symbol}] := Block[inds, Unevaluated[code] /. MapIndexed[ Function[{i, pos}, pos /. {p_} :> (i :> #2[[p]])], ...


13

I think Mr. Wizard provided a very thorough answer to the question. I would however like to add a slight example of wrapping this up nicely in a format similar to Function[] but using destructuring: SetAttributes[dFunction, HoldAll] dFunction[pattern_, body_][arg___] /;MatchQ[{arg}, pattern] := {arg} /. pattern :> body This then allows you to have nice ...


11

One use case that I've been exploring has been to replace Mathematica's own Compile functionality with (potentially much faster) generated C which can be compiled using CCompilerDriver` and linked back in using LibraryFunctionLoad. The built-in Compile is a very nice piece of functionality, but if you examine the generated C output for many ...


10

ToCCodeString has the "Indent" option, which defaults to None. Set it to the number of tabs you want for an indentation, and you should be fine.


9

Yves Papegay has developed SymbolicC in order to automate the conversion of his models to C. This is used by Airbus. For more information see his presentations in the Wolfram Library Archive http://library.wolfram.com/infocenter/search/?search_results=1&search_person_id=6281 especially this one http://library.wolfram.com/infocenter/Conferences/7477/ ...


9

OK, FrontEnd: After creating the "overview" button below just click first on your function definition and then on the button and under the function definition you will get (nearly) what you want, i.e. : Part 1. Step 1. Local variables Part 1. Step 2. Option variables Part 2. Step 1. Check for null Input Part 2. Step 2.:1 Check for groups Part 2. ...


8

I would suggest something more simple-minded than Rolf's sophisticated implementation: Clear[extractComments]; extractComments[boxes_] := StringJoin@ Riffle[ Cases[boxes, r : RowBox[{"(*", ___, "*)"}] :> StringJoin@Cases[r, _String, Infinity], Infinity], "\n"]; and the palette: CreatePalette[ ...


8

Check the CUDALink/OpenCLLink documentation, they have some creative applications http://reference.wolfram.com/mathematica/CUDALink/tutorial/Applications.html#818090 http://wolfram.com/xid/0isq3flowdud5n74bny881he6-0wsz71 http://wolfram.com/xid/0d195cdqgdtuw44ioyvm6og9pu-bv64sq Also the webinars have other examples.


7

I feel slightly foolish for posting this, because I figured it our almost immediately after posting my question, but the answer is to follow C's philosophy that type declaration should mimic use. CFunction is used only for function declaration and definition; for function types, you use CCall: CTypedef["double", CCall[CDereference[rhs], {"double", ...


6

This is a very very specific example. There's something I wanted to do for a while but I didn't have time yet. Exporting 3D graphics into PDF produces lots and lots and lots of triangles (for the gradients), which aren't joined properly. The result is that rendering time in a PDF reader is very long, the PDF size is very large, and the whole result just ...


4

Unfortunately, it does not appear that this is possible without the Enterprise edition of Mathematica (or, perhaps, an alternative conclusion is that Mathematica is not the best tool for this job). As Mr. Wizard points out: I'm afraid you are in for disappointment. Apart from the Enterprise Edition or Wolfram Player Pro Mathematica does not have this ...


3

As it turns out, the answer is $InstallationDirectory/SystemFiles/IncludeFiles/C/. Thanks Heike!


2

My first answer explains that Mathematica's replacement rules perform destructuring. This answer is intended to complement jVincent's method, which I see is appreciated. My aim is to provide Attributes for the pattern-based function. This requires that the head evaluate therefore SubValues may not be used. Here are two separate approaches. Module A ...


2

You can use Subtract or Differences to get the differences in the index and use FreeQ to test if every element is True. This removes the need for the ugly #2[[1]]-#2[[2]] and also the Flatten and And@@ at the end. triDiagonalQ[mat_?MatrixQ] :=FreeQ[MapIndexed[(#1 == 0 || Abs[Subtract @@ #2] <= 1) &, mat, {2}], False] The pattern test ...


2

Further possibilities that I can think of might be Genetic Programming. Code obfuscation Individualization of source code in order to track piracy.


2

Actually Leonid didn't use SymbolicC in his answer yesterday, he mentioned it as another more powerful way to potentially deal with my question. I would assume that at some stage of Compile[...,CompilationTarget->"C"] that Mathematica builds an internal expression using SymbolicC. It would be nice to be able to "intercept" it at this stage. To solve my ...


2

I am not going to try to understand everything you wrote, and I shall not attempt a full breakdown of your code as I have done for other questions tagged code-review, unless specifically requested, as it seems to me you have a working solution with a simple flaw: you are not evaluating the body of the Function that you are generating. To that end you could ...


1

In your code you have a Symbol s that is not declared before the line Begin line. Because of this it is created in that specified Afak10`Private` context. (Also, your code presently has an error; it needs to be Begin["Private`"].) You can include the Global` context if that is in fact the Symbol you wish to return: f[x_] := Module[{}, x^2 + Global`s]; ...


1

SymbolicC is incredibly effective for writing boilerplate code, or really anything that is largely repetitive. You can use the powerful and intuitive pattern matching and manipulation functions built-in to Mathematica to build up a computation tree using SymbolicC symbols, then transform the tree into a fully-functional C file by passing the tree to ...


1

Perhaps you could just use With in a Function? lst = ConstantArray[0, {3, 3}]; MapIndexed[ Function[{value, pos}, With[{i = pos[[1]], j = pos[[2]]}, {value, i, j} ] ], lst, {2}]



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