11
votes
Accepted
Metaprogramming: creating compiled functions from inter-dependent code blocks
To compose code from blocks you could use simple quoting mechanism like following:
...
9
votes
Accepted
Converting Piecewise function to C code
-10 < x < 0 // LogicalExpand // CForm
(* -10 < x && x < 0 *)
8
votes
Accepted
Compute Hessian of function symbolically
I think this should be fine, the full form doesn't look neat with x$123 etc but that doesn't matter:
...
7
votes
Accepted
Easy code help about matrix. How to change the elements in a given matrix?
Have a look at ReplacePart[]
A[1, 1] = {1, 2, 3, 4};
ReplacePart[A[1, 1], 2 -> 5]
{1, 5, 3, 4}
6
votes
Accepted
How to get a peek at the source code for demonstrations if there is no source code provided
One method that I think works is to highlight the demonstration, copy it to a new Mathematica notebook, and then click "Convert to Input Form" on the cell.
6
votes
Christmas fun with Mathematica
Nothing groundbreaking, but a quick composure of some graphics objects:
...
6
votes
Metaprogramming: creating compiled functions from inter-dependent code blocks
You already have a very good and general answer for your question. Just for completeness I wanted to show you an alternative which is much less general but shows that there is actually not too much ...
6
votes
Compute Hessian of function symbolically
Is this not a good place to use Formal Symbols? A number of built-ins do that.(1)
...
6
votes
Accepted
Understanding code generated by MicrocontrollerEmbedCode
The value 84.38063590995105=-26.8592*π is a residue that pops up if the equilibrium value is not set correctly.
There are a few things that need to be changed to ...
6
votes
How to generate input cells whose code is determined dynamically?
You should just use With instead of Module, as Carl Woll suggests in the comments:
...
5
votes
Accepted
5
votes
Accepted
How to convert any string of code to an input cell?
TLDR: FrontEnd will happily accept an input form string wrapped with BoxData:
...
5
votes
Stepped Infinite Square Well
Having solved the problem with writing the potential, you may use "Eigensystem" to solve this problem (with arbitrary numerical values):
...
4
votes
SymbolicC`CSwitch basically broken?
The documentation for CSwitch looks like it was written by somebody who didn't understand what the SymbolicC package or, for ...
4
votes
Write Power in CForm[] as multiplication instead
You can override the default CForm handling of Power as follows:
...
4
votes
NDSolveValue solutions differ based on number of equations
General reasons for a difference
There are (at least) two differences why solving independent systems with one call might result in different solution than solving them separately:
Independent vs. ...
4
votes
Accepted
4
votes
How to programmatically modify a function definition
This is my first time wading into metaprogramming in Mathematica, so take this with a pinch of salt. I can get the DownValues of ...
4
votes
Accepted
How to programmatically modify a function definition
To operate on notebooks
Let nb be the notebook you want to alter obtained with NotebookGet[]. For instance, it could be ...
4
votes
How to generate input cells whose code is determined dynamically?
This required a bit more trickery than I would like:
...
Community wiki
3
votes
Accepted
3
votes
How can I automatically generate usage messages?
Here's a partial implementation, working from the DownValues and friends:
We start with a bunch of DownValues cleaning:
...
3
votes
Accepted
Optimizing an expression – find identical subexpressions and substitute them
As I noted in my comment above, the undocumented (thus, caveat emptor as usual) method is to use Experimental`OptimizeExpression[]:
...
3
votes
SymbolicC namespace?
Have you looked at the builtin documentation?
SymbolicC User Guide
The introduction – C++ section clearly states:
At present, SymbolicC only supports C syntax.
That is, it does not support C++.
...
3
votes
Metaprogramming: creating compiled functions from inter-dependent code blocks
I came up with my own way of dealing with this. I'm not sure if it's the simplest way. I was careful to make sure I dealt with all possible namespace issues, as well as possible recursive calls ...
3
votes
Write Power in CForm[] as multiplication instead
If the symbol you are using is known in advance, you can useTagSetDelayed and associate an UpValue with that symbol inside the <...
3
votes
Applying And to a list inside a Function
For the exact expansion that you show:
Replace[And[1, 3, 7, 9, 13, 27] &, x_ :> PrimeQ[# + x], {2}]
...
3
votes
OptimizeExpression to C code
So since I need it, I wrote the function myself. It is a little bit hackish approach, but it solves my problem. So any comment and suggestions for improvement are welcome.
...
3
votes
How to programmatically modify a function definition
That's not a real answer... But I failed to format it as a comment. Again.
You're opening a deep can of worms now, called "metaprogramming".
Please search for "metaprogramming" and ...
3
votes
Accepted
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
code-generation × 91code-request × 13
compile × 8
code-review × 8
plotting × 5
list-manipulation × 5
differential-equations × 5
programming × 5
meta-programming × 5
equation-solving × 4
graphics × 4
functions × 4
symbolic × 4
code × 4
c-codegenerator × 4
calculus-and-analysis × 3
export × 3
evaluation × 3
notebooks × 3
cells × 3
input × 3
matrix × 2
function-construction × 2
simplifying-expressions × 2
mathematical-optimization × 2