Skip to main content
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: ...
jkuczm's user avatar
  • 15.1k
9 votes
Accepted

Converting Piecewise function to C code

-10 < x < 0 // LogicalExpand // CForm (* -10 < x && x < 0 *)
xzczd's user avatar
  • 67.7k
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: ...
Kuba's user avatar
  • 137k
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}
infinitezero's user avatar
  • 1,441
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.
MassDefect's user avatar
  • 10.2k
6 votes

Christmas fun with Mathematica

Nothing groundbreaking, but a quick composure of some graphics objects: ...
ktm's user avatar
  • 4,302
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 ...
Albert Retey's user avatar
  • 23.6k
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) ...
Mr.Wizard's user avatar
  • 273k
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 ...
Suba Thomas's user avatar
  • 8,806
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: ...
Alexey Popkov's user avatar
5 votes
Accepted

Is it possible to automate definitions given with TagSetDelayed?

You mean something like: ...
Sjoerd Smit's user avatar
  • 24.3k
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: ...
Kuba's user avatar
  • 137k
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): ...
Daniel Huber's user avatar
  • 54.6k
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 ...
m_goldberg's user avatar
  • 108k
4 votes

Write Power in CForm[] as multiplication instead

You can override the default CForm handling of Power as follows: ...
Carl Woll's user avatar
  • 131k
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. ...
Michael E2's user avatar
  • 241k
4 votes
Accepted

Turn definitions into rules

How about: ...
xzczd's user avatar
  • 67.7k
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 ...
flinty's user avatar
  • 25.6k
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 ...
Michael E2's user avatar
  • 241k
4 votes

How to generate input cells whose code is determined dynamically?

This required a bit more trickery than I would like: ...
3 votes
Accepted

How generalizes (automate) recursive code

One way: ...
C. E.'s user avatar
  • 71k
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: ...
b3m2a1's user avatar
  • 47k
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[]: ...
J. M.'s missing motivation's user avatar
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++. ...
Szabolcs's user avatar
  • 236k
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 ...
Ben Niehoff's user avatar
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 <...
Stitch's user avatar
  • 4,225
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}] ...
Mr.Wizard's user avatar
  • 273k
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. ...
tom's user avatar
  • 295
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 ...
Pavel Perikov's user avatar
3 votes
Accepted

Have Button fire with both Kernel and FrontEnd

Is this what you are looking for? ...
Rohit Namjoshi's user avatar

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