<div dir="ltr"><div>Thanks for replying. Here are some details that may be helpful in understanding the problem.</div><div><br></div>I am using pythonnet to call python code from c#. This involves doing something of this sort:<div>
<br></div><div><div>            PythonEngine.Initialize();</div><div>            PyObject testModule = PythonEngine.ImportModule("myModule");</div><div>            </div><div>            var zero = new PyFloat(0.0);</div>
<div>            var Lock = PythonEngine.AcquireLock();</div><div>            var f = testModule.InvokeMethod("myModule", new PyObject[1] { zero });</div><div>            var g = (double)f[0].AsManagedObject(typeof(double));</div>
<div>            PythonEngine.ReleaseLock(Lock);<br></div><div>            PythonEngine.Shutdown();</div></div><div><br></div><div>This passed a python float with a value of 0 to the python module. Now if my module, uses numpy arrays I need to pass at best a python list that can be converted to a numpy array. The python list can be created using new PyList and then assigning every double in the c# array into the pylist. This should be inefficient I presume. So one way that I thought was to marshal this into an unmanaged array and then pass the unmanaged array into python. The python program can use <span style="color:rgb(80,0,80)">numpy.ctypeslib.as_array on the unmanaged array. Unfortunately, while calling python from c# I can only pass a pyobject array while calling invokemethod - so that won't work.</span></div>
<div><span style="color:rgb(80,0,80)"><br></span></div><div><font color="#500050">In short: I want to call python code from a c# program. My python code uses numpy arrays. I have a multidimensional array of doubles in the c#program that I would like to pass to the python code.</font></div>
<div><font color="#500050"><br></font></div><div><font color="#500050">Basically, the linked post already had the c# array in python using the clr module. But in my case when I call python from c#, I do that using InvokeMethod that would only allow me to pass an array of PyObjects. So that's the challenge.</font></div>
<div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Aug 20, 2014 at 12:23 PM, Jeffrey Bush <span dir="ltr"><<a href="mailto:jeff@coderforlife.com" target="_blank">jeff@coderforlife.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Numpy arrays are almost always created in Python and there are dozens of methods. That post you link to has the best solution for this case:<br>
<br>import numpy as np<br>dest = np.empty(len(src))<br><br>That creates a new numpy array of the same length of src with undefined ("empty") values at every index. You could substitute len(src) for whatever length you want (or a tuple if you want something not 1D). We use empty since we are about to set every value to something so we don't care that they have undefined values at that point.<div>


<div><br></div><div>Overall I don't understand your problem, can you be more clear? I don't know if you are trying to do the copy in Python or C#, I am guessing you are copying from C# array to a new Python NumPy array, which is exactly what that post did (in Python).</div>


<div><br></div><div>Jeff</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div class="h5">On Wed, Aug 20, 2014 at 10:21 AM, Mika S <span dir="ltr"><<a href="mailto:siddhupiddu@gmail.com" target="_blank">siddhupiddu@gmail.com</a>></span> wrote:<br>


</div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div class="h5"><div dir="ltr">How do I pass a double array as a numpy list from C# (calling python code from c#) or passing something that can become a numpy list with minimal overhead.<div>


<br></div><div><div>Right now, the naive way would be to create a new PyList. And to fill it with elements from the double array. <br>
</div><div><br></div><div>But I read on a thread on this list (<a href="https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html" target="_blank">https://mail.python.org/pipermail/pythondotnet/2014-May/001525.html</a>) that Marshal.copy was faster. </div>



<div>So, I tried Marshal.copy but I can't figure out how to instantiate a numpy array in python. Numpy can instantiate a new array from native C array by calling numpy.ctypeslib.as_array.</div><div><div>So, theoretically if i can do Marshal.Copy(csharparray,0,addrofnative,csharparray.Length) and pass the addrofnative (which is a IntPtr) I could be done. But I can only pass PyObject when calling a module function.</div>



</div></div><div><br></div><div>Any ideas ?</div></div>
<br></div></div>_________________________________________________<br>
Python.NET mailing list - <a href="mailto:PythonDotNet@python.org" target="_blank">PythonDotNet@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pythondotnet" target="_blank">https://mail.python.org/mailman/listinfo/pythondotnet</a><br></blockquote></div><br></div>
<br>_________________________________________________<br>
Python.NET mailing list - <a href="mailto:PythonDotNet@python.org">PythonDotNet@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pythondotnet" target="_blank">https://mail.python.org/mailman/listinfo/pythondotnet</a><br></blockquote></div><br></div></div>