Automatic Field Order Rearrangement in D - class

I just saw the following at http://dlang.org/pretod.html#pragmapack quote:
For D classes, there is no need to adjust the alignment (in fact, the compiler is free to rearrange the data fields to get the optimum layout, much as the compiler will rearrange local variables on the stack frame). For D structs that get mapped onto externally defined data structures, there is a need, and it is handled with:
struct Foo
{
align (4): // use 4 byte alignment
...
}
Does this mean that structs fields are not rearranged as are class fields mentioned above?

Yes, although I don't think current implementations rearrange class fields anyway.

Related

Tell IPython to use an object's `__str__` instead of `__repr__` for output

By default, when IPython displays an object, it seems to use __repr__.
__repr__ is supposed to produce a unique string which could be used to reconstruct an object, given the right environment.
This is distinct from __str__, which supposed to produce human-readable output.
Now suppose we've written a particular class and we'd like IPython to produce human readable output by default (i.e. without explicitly calling print or __str__).
We don't want to fudge it by making our class's __repr__ do __str__'s job.
That would be breaking the rules.
Is there a way to tell IPython to invoke __str__ by default for a particular class?
This is certainly possible; you just need implement the instance method _repr_pretty_(self). This is described in the documentation for IPython.lib.pretty. Its implementation could look something like this:
class MyObject:
def _repr_pretty_(self, p, cycle):
p.text(str(self) if not cycle else '...')
The p parameter is an instance of IPython.lib.pretty.PrettyPrinter, whose methods you should use to output the text representation of the object you're formatting. Usually you will use p.text(text) which just adds the given text verbatim to the formatted representation, but you can do things like starting and ending groups if your class represents a collection.
The cycle parameter is a boolean that indicates whether a reference cycle is detected - that is, whether you're trying to format the object twice in the same call stack (which leads to an infinite loop). It may or may not be necessary to consider it depending on what kind of object you're using, but it doesn't hurt.
As a bonus, if you want to do this for a class whose code you don't have access to (or, more accurately, don't want to) modify, or if you just want to make a temporary change for testing, you can use the IPython display formatter's for_type method, as shown in this example of customizing int display. In your case, you would use
get_ipython().display_formatter.formatters['text/plain'].for_type(
MyObject,
lambda obj, p, cycle: p.text(str(obj) if not cycle else '...')
)
with MyObject of course representing the type you want to customize the printing of. Note that the lambda function carries the same signature as _repr_pretty_, and works the same way.

'Find' and 'unique' in an array of user-defined class objects (MATLAB)

After dabbling in C# I'm now keen to use some OOP in Matlab - to date I've done none!
Let's say that I define a class for a data object with a handful of properties...
classdef TestRigData
properties
testTemperature
sampleNumber
testStrainAmplitude
sampleMaterial
rawData
end
methods
% Some constructors and data manipulation methods in here
end
end
...where 'rawData' would be a m-by-n array of actual experimental data, and the other values being doubles or strings to help identify each specific experiment.
If I have an array of TestRigData objects, what would be the best way of finding the indices of objects which meet specific criteria (e.g. testTemperature == 200)? Or getting all the unique values of a property (e.g. all the unique sample numbers in this collection).
If they were arrays of their own, (myNewArray = [3 5 7 22 60 60 5]) it would be easy enough using the find() and unique() functions. Is there a class I can inherit from here which will allow something like that to work on my object array? Or will I have to add my own find() and unique() methods to my class?
You can assign an ID value (a hash value in the general case) to TestRigData objects and store it as a new property. You can then extract all ID values at once to a cell array, e.g {yourarray.id} (or [yourarray.id] if the ID values are scalars), allowing you to apply find and unique with ease.
Adding your own find and unique is definitely possible, of course, but why make life harder? ;)
The suggestion of creating this as a handle class (rather than value class) is something I need to think about more in the future... after having put together some initial code, going back and trying to change classdef TestRigData to classdef TestRigData < handle seems to be causing issues with the constructor.
Bit unclear of how I would go about using a hash value unique to each object... but the syntax of extracting all values to an array is ultimately what got me in the right direction.
Getting a new object array which is the subset of the original big data array conforming to a certain property value is as easy as:
newObjectArray = oldObjectArray([oldObjectArray.testTemperature]==200);
Or for just the indices...
indicesOfInterest = find([oldObjectArray.testTemperature]==200);
Or in the case of non-scalar values, e.g. string property for sample material...
indicesOfInterest = find(strcmpi({oldObjectArray.sampleMaterial},'steel'));

Phobos-Range Compatible Fixed-Dimensional Vector

What is the most clever way of implementing a fixed-dimensional vector in D that is compatible with the RandomAccessRange interface in Phobos? Do I have to reimplement all the members opIndex, length etc or is the cleverer way through delegation, alias this or template mixins? I've been looking at a couple of fixed-size vector structs on github D projects but none seem to care about being compatible with Phobos ranges. Update: Is just read that containers should be reference types so I guess this isn't the way to do it in D right?
If your vector has continuous internal storage then you can just return a slice of that data from opSlice():
struct Vector
{
private real[4] data;
auto opSlice() { return data[]; }
}
Containers don't have to be reference types, but either way using opSlice is the usual way to get a range from a container.

Is there a way to remove all but a few desired fields from a struct in MATLAB?

So I have several structs that contains data that is used is a dozen or so scripts. The problem is that for each script I only need a handfull of variables and the rest I can ignore. I am using a massive amount of data (gigs of data) and MATLAB often gives me out of memory errors so I need to remove all unnecessary fields from the structs.
Currently I have a cell that contains all unneeded fields and then I call rmfield on the structs. But the fields in the structs often change and it is getting to be a pain to be constantly updating the list of unneeded fields. So is there a way to tell MATLAB to keep only those fields I want and remove everything else even if I don't know what everything else is?
Here is an example,
Struct 1 has: A, B, C, D, E fields
Struct 2 has: A, B, C, D, E, F fields
Struct 3 has: A, B, C, D, E, F, G, H, I fields
Sometimes Struct 3 might only have A thru G.
I want to keep only A, B, and C fields and remove all other data from all the structs.
Here is one way to do it:
Get the list of all fieldnames using fieldnames
Remove the ones that you want to keep from the list
Remove everything that is left in the list
Example
s.a=1
s.b=2
s.c=3
s.d='chitchat'
tokeep = {'a','b'}
f=fieldnames(s)
toRemove = f(~ismember(f,tokeep));
s = rmfield(s,[toRemove])
You could copy your struct's desired fields to a new variable in a function.
function newVar = getABC(strct)
newVar.A = strct.A;
newVar.B = strct.B;
newVar.C = strct.C;
end
strct will not be copied in memory beacuse you will not be manipulating it.
MATLAB uses a system commonly called "copy-on-write" to avoid making a
copy of the input argument inside the function workspace until or
unless you modify the input argument. If you do not modify the input
argument, MATLAB will avoid making a copy.
You can get newVar and then clear strct from memory.
Fred's generalized version:
function newVar = getFields(oldVar, desiredCell)
for idx = 1:length(desiredCell)
newVar.(desiredCell{idx}) = oldVar.(desiredCell{idx});
end
1) Let's say you a have a structure S
2) You want to keep only the first three fields of S and delete all the others
fieldsS = fieldnames(S);
S = rmfield(S,fieldsS(4:end));
The rmfield method in MATLAB is rather slow, so when dealing with large structures it is best to avoid it.
This MATLAB file exchange item: kpfield is basically the inverse of rmfield and should work exactly as you require.
It converts the structure to a cell array before keeping only the required indices by creating a logical array based on whether the fields exist in the fieldnames or not. The modified cell array is then converted back to a structure.
Disclaimer: I have written kpfield as I came across exactly the same issue.
I had success loading the struct- physically deleting fields I wanted to remove and then resaving the struct.
Deleting fields in the workspace does not delete them from the original struct - so resaving is necessary.

What does the . operator do in matlab?

I came across some matlab code that did the following:
thing.x=linspace(...
I know that usually the . operator makes the next operation elementwise, but what does it do by itself? Is this just a sub-object operator, like in C++?
Yes its subobject.
You can have things like
Roger.lastname = "Poodle";
Roger.SSID = 111234997;
Roger.children.boys = {"Jim", "John"};
Roger.children.girls = {"Lucy"};
And the things to the right of the dots are called fields.
You can also define classes in Matlab, instatiate objects of those classes, and then if thing was one of those objects, thing.x would be an instance variable in that object.
The matlab documentation is excellent, look up "fields" and "classes" in it.
There are other uses for ., M*N means multiploy two things, if M, N are both matrices, this implements the rules for matrix multiplication to get a new matrix as its result. But M.*N means, if M, N are same shape, multiply each element. And so no like that with more subtleties, but out of scope of what you asked here.
As #marc points out, dot is also used to reference fields and subfields of something matlab calls a struct or structure. These are a lot like classes, subclasses and enums, seems to me. The idea is you can have a struct data say, and store all the info that goes with data like this:
olddata = data; % we assume we have an old struct like the one we are creating, we keep a reference to it
data.date_created=date();
data.x_axis = [1 5 2 9];
data.notes = "This is just a trivial example for stackoverflow. I didn't check to see if it runs in matlab or not, my bad."
data.versions.current = "this one";
data.versions.previous = olddata;
The point is ANY matlab object/datatype/whatever you want to call it, can be referenced by a field in the struct. The last entry shows that we can even reference another struct in the field of a struct. The implication of this last bit is we could look at the date of creation of the previous verions:
data.versions.previous.date_created
To me this looks just like objects in java EXCEPT I haven't put any methods in there. Matlab does support java objects which to me look a lot like these structs, except some of the fields can reference functions.
Technically, it's a form of indexing, as per mwengler's answer. However, it can also be used for method invocation on objects in recent versions of MATLAB, i.e.
obj.methodCall;
However note that there is some inefficiency in that style - basically, the system has to first work out if you meant indexing into a field, and if not, then call the method. It's more efficient to do
methodCall(obj);