Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.7K Views
|
5 Replies
|
1 Total Like
View groups...
Share
Share this post:

Create a table of unknown size in a Compile function?

Posted 10 years ago

I would like to write a compile function with a Matrix of unknown size(at the moment of the compilation) similar to :

Fx = Compile[{{matrix,_Real},{size,_Integer},Block[{M},
M = Table[{0,0},size];
.... rest of the function ....
];];

The problem is that the compilation failed because the function Table requires a value and not a variable. I absolutely need to initialize this matrix of zeros in the rest of the code, and I want a Compile function in order to save time. Does someone know how to fix this problem?

Thank's in advance Q.A.

POSTED BY: Quentin Ansel
5 Replies
Posted 7 years ago
POSTED BY: Hui Ren

This doesn't fail in any version I try:

cf = Compile[{{size, _Integer}},
  Block[{m},
   m = Table[{0, 0}, {size}]
   ]
  ]

Please describe precisely what the problem is. Give a minimal but complete example.

POSTED BY: Szabolcs Horvát

Hi Quentin,

I cannot really run the code because as you say it is incomplete. One observation is that you use procedural style, like in C or Fortran or so. My guess is that you "speak" one of those. The thing is that in Mathematica there are other styles, such as functional or rule based.

It turns out that functional style is quite optimised and usually faster than procedural. Given your description of the problem, I suspect that the speed gain that you might achieve like that would be substantial. You have three nested loops and getting rid of some/all of them should help.

Cheers,

Marco

POSTED BY: Marco Thiel
POSTED BY: Quentin Ansel

Hi Quentin,

this does not answer you question but have you tried optimising that within Mathematica, i.e. without compilation first? Using the Table command is actually quite slow:

Table[0, {40000}, {40000}]; // AbsoluteTiming

takes 43.5 seconds on my computer.

ConstantArray[0, {40000, 40000}]; // AbsoluteTiming

and

Normal[SparseArray[{}, {40000, 40000}]]; // AbsoluteTiming

run about 7.95 seconds. If you can use a SparseArray only it becomes really fast:

SparseArray[{}, {40000, 40000}]; // AbsoluteTiming

Also further calculations on SparseArray are quite fast: 0.000203 seconds.

Cheers,

Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard