Acessing a value on an ArrayFire array - arrayfire

I had a af::array and need to acess the value in a row, like z(8), e.g.
But, when i try to do this, in a loop with a incremental i, the compiler returns
cannot convert from 'af::array::array_proxy' to 'double'
Someone knows how can I acess the value inside the array?
Thanks.

Take a look at scalar in the arrayfire documentation here.
For your use case, you can access the element like this :
float val = z(8).scalar<float>();
Could you talk about why you need to do this? In many cases, transferring values to the host from the GPU is unnecessary and avoidable.

Related

How to declare a matrix or a vector giving its dimensions?

Is it possible in Maxima for me to declare a matrix with a given number of rows and of columns without initializing it? The closest thing I found is the function zeromatrix. And how to declare vectors like that?
I want to have them declared so I can fill them in loops.
Thank you.
You don't need to declare things in Maxima prior to using them.
For instance, you could create some lists containing the rows, as in
for i:1 thru 6 do myrow[i]:makelist(i+j,j,1,5);
Then you could construct the matrix with
M[i,j]:=myrow[i][j]
and
genmatrix(M,6,5)
Of course, this can be made more precise if you provide more details of what you are trying to do...

Why isn't "values" property in a Dictionary a simple Array, but instead it's something weird?

let dict = [1:"One", 2:"Two", 3:"Three"]
let values = dict.values
print(values.dynamicType)
prints:
LazyMapCollection<Dictionary<Int, String>, String>
There are two things I don't understand here. Wouldn't it be a bit more simple if values returned an Array? Also, what is LazyMapCollection? I looked into Apple's reference but it provides literally no information (or nothing I am able to make sense of).
You can iterate over this object, because it is CollectionType:
for v in values {
print(v)
}
prints:
Two
Three
One
But for some reason Apple didn't use Array type.
A LazyMapCollection is a lazy (only evaluated if necessary) view on a collection. By "view" I mean "window", "frame", "virtual subset", this kind of concept.
To get the actual values from it, just use the Array initializer:
let values = dict.values
let result = Array(values)
You have here a serious speed optimisation. Creating an array is expensive. Instead you get an instance of some strange class that behaves in every way like an array. However, it doesn't have its data stored in a real array, instead it access the data from a dictionary.
Say you have a dictionary with 10,000 string values. You don't want iOS to copy all the 10,000 string values when you call dict.values, do you? That's what this class is there for, to prevent copying 10,000 strings. A real array would force copying.
And anyway, with your username you are asking for things like this, and Apple provides many examples. That's how they make iOS fast.
Both arrays and dictionaries are value types (structs). That means that once you request an array, the values must be copied. If Dictionary.values were returning an array, this could be a performance expensive operation - and one that is usually not needed because most of the times you want only to iterate over the values.
So, to use a special (lazy) collection type is basically a way to prevent copying when the copying is not needed. If you want a copy, you have to ask for it explicitly.

Increasing the length of a tuple in Erlang

How can I increase the length of a tuple in Erlang? For example, suppose Tup={1,2,3}, and now I want to add another element to it. Is there any way to do this?
Tuple is not suppose to be a flexible data structure. If you are resizing it often, then you should consider using other erlang data structures like lists, maps or sets - depends on your expectation. Here is nice introduction to key-value stores.
But if you really have to extend that tuple, then you can use erlang:append_element/2:
{1,2,3,4} = erlang:append_element({1,2,3}, 4).
Tuples aren't mutable so you can't, strictly speaking, increase the length.
Generally, if you want a variable-number-of-things datatype, a tuple will be very inconvenient. For example, iterating over all elements of a list is highly idiomatic, while iterating over all elements of a tuple whose size is unknown at compile-time is a pain.
However, a common pattern is to get a tuple as a result from some function and return elements of that tuple plus additions.
country_coords(Name) ->
{Lat, Lng} = find_address(Name),
{_Street, _City, _Zip, Country} = geocode(Lat, Lng),
{ok, Lat, Lng, Country}.
erlang:append_element(tuple_whose_length_to_increase, element_to_be).This is the inbuilt function but tuples,lists are not meant to be flexible.So avoid using this function unless there is no other way

Passing open array into an anonymous function

What is the least wasteful way (i.e. avoiding copying if at all possible) to pass the content of an open string array into an anonymous function and from there into another function that expects an open array?
The problem is that open arrays cannot be captured in anonymous functions in Delphi XE2.
This illustrates the problem:
procedure TMyClass.DoSomething(const aStrings: array of string);
begin
EnumItems(
function (aItem: string) : Boolean
begin
Result := IndexText(aItem, aStrings) >= 0;
end);
end;
The compiler complains: "Cannot capture symbol 'aStrings'".
An obvious solution is to make a copy of aStrings in a dynamic array and capture that. But I don't want to make a copy. (While my specific problem involves a string array and making a copy would only copy the pointers not the string data itself due to reference counting, it would also be instructive to learn how to solve the problem for an arbitrarily large array of a non-reference counted type.)
So I tried capturing instead a PString pointer to the first string in aStrings and an Integer value of the length. But then I couldn't figure out a way to pass these to InsertText.
One other constraint: I want it to be possible to call DoSomething([a, b, c]).
Is there a way to do this without making a copy of the array, and without writing my own version of IndexText, and without being hideously ugly? If so, how?
For the sake of this question I've used IndexText, but it would be instructive to find a solution for a function that could not be trivially rewritten to accept a pointer and length parameter instead of an open array.
An acceptable answer to this question would be: No, you can't do that (at least not without making a copy or rewriting IndexText) though if so I'd also like to know the fundamental reason why not.
If you don't want to copy the array then you should change the signature of DoSomething to take a TArray<string> instead. You of course have to change the caller side if you are passing the elements directly (only since XE7 you can pass dynamic arrays in the same way) - like DoSomething([a, b, c]) i mean.
My advice is not to mess around with some internal pointers and stuff, especially not for an open array.
There's no way to do this without making a copy. Open arrays cannot be captured as you have found, and you cannot get the information into the anonymous method without capture. You must capture, in general, because you need to extend the life of the variable.
So, you cannot do this with an open array and avoid a copy. You could instead:
Switch from open array to a dynamic array, TArray<string>.
Make a copy of the array. You would not be copying the string data, just the array of references to the strings.

Using native pointers in F# with WritableBitmap

Hello
I am trying to use WritableBitmap.BackBuffer as used in this example, see the section examples.
What I am trying to do really is write an int[] into a space a nativeint points to.
To be able to write from int [] to some memory I started from this answer on SO.
Microsoft.FSharp.NativeInterop.NativePtr.write seems to be a good function to use to write.
After trying and reading a bit two questions arises.
WritableBitmap.BackBuffer has the type nativeint, how to convert to nativeptr that NativePter.write wants?
It seems that I can only write one int at a time but I want to write a whole int [].
I admit that I am in deep water but it is in the deep water you learn to swim :)
Thank in advance
Gorgen
I think that the NativePtr.write function can be only used to write single value at a time, so if you want to copy an array, you'll have to use a for loop.
An easier option may be to use the Marshal.Copy method (see MSDN) which takes a source array (there are overloads for arrays containing elements of various types) and intptr as the destination.
Something like this should work:
let imageData = [| ... |] // generate one dimensional array with image data
writeableBitmap.Lock()
let buffer = writeableBitmap.BackBuffer
Marshal.Copy(imageData, 0, buffer, imageData.Length)
I think Tomas's suggested approach makes sense. However, to answer your first question, you can convert a nativeint to a nativeptr using the NativeInterop.NativePtr.ofNativeInt function.

Resources