Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8121

Compile function and AppendTo for lists (vrs. 8.0.4)

From kris <kristophs.post@web.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Compile function and AppendTo for lists (vrs. 8.0.4)
Date 2012-01-24 10:07 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jflvt0$jpr$1@smc.vnet.net> (permalink)

Show all headers | View raw


Hi

I have some trouble with my code and would like ask for your help. In
general it is about appending data in form of a list to an existing
list in a compiled function. As far as I understand Mathematica is not
supporting what I am asking for. In order to understand the problem in
more detail I present some "toy" code below.

test=Compile[{{k,_Integer}},
Module[{mat={}},
For[i=1,i<=k,i++,
AppendTo[mat,{1,i}];
];
mat
]
];

test[2]
(*which produces an error*)

Appending data in form of numbers for example works just fine but not
with lists. Can anybody explain why Mathematica does not support
appending lists to lists for compiled function? As an alternative I
tried Reap and Sow, which also produces an error.

However, what seems funny is the following code:

mat={};
test=Compile[{{k,_Integer}},
For[i=1,i<=k,i++,
AppendTo[mat,{1,i}];
];
];

test[2];mat

The above code produces the result that I was looking for in a
cumbersome way. I would like to prefer compile code which produces the
same result without calling the list mat again.

Thanks for help I do appreciate it.
Cheers,
Kris

Back to comp.soft-sys.math.mathematica | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Compile function and AppendTo for lists (vrs. 8.0.4) kris <kristophs.post@web.de> - 2012-01-24 10:07 +0000
  Re: Compile function and AppendTo for lists (vrs. 8.0.4) "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2012-01-25 12:17 +0000

csiph-web