Execute a "prepared" math operation in Objective-C - iphone

I want to to math operations with some kind of prepared formula that would look like tan(%f)*1000 or %f+%f where the %f has to be replaced by an argument.
Is there a function in Objective-C that I can pass the format of my formula and the required numbers to execute this prepared operation?
I hope the problem is described understandable, if not, leave a comment.
Thanks in advance.
Edit 1: Thanks for your answers so far, but I'm looking for something more dynamic. The block and inline function is great, but to static. I also understand that this may be something hard to achieve out of the box.

You may be interested in DDMathParser, found here. I believe it will do everything you're looking for.

There is nothing that would do it this way, however what you could do is rewrite your "format" into a function, and just pass the arguments it needs to have, much faster and much easier.
inline float add(float p_x,float p_y)
{ return p_x+p_y; }
inline is a compiler feature that you can use to speed things up. It will replace the function call with the code it executes when you compile. This will result in a lager binary though.

If I understand your question correctly, Objective-C Blocks are great for this.
typedef double (^CalcBlock)(double);
CalcBlock myBlock = ^(double input) {
return (tan(input) * 1000);
};
NSLog(#"Result: %f", myBlock(M_PI_2));
You can pass the block that contains your algorithm to other objects or methods.

Related

Set Matlab function parameter as uint8

Is it possible in Matlab to say what the function expects? something like this:
function functionA( obj, uint8(param) )
Here I am saying that the function expects one parameter of type uint8.
Not on the function signature. Typically, you do this via an assert block:
function (obj, param)
assert(isa(param, 'uint8'),...
[mfilename ':invalid_datatype'],...
'Parameter ''param'' must be of class ''uint8''; received ''%s''.',...
class(param));
To complement Rody's answer, there are four ways that you can do this:
Use a conditional and raise an exception if the argument is not of the expected type. The problem with this method is that you have to write a lot of code.
Use an assertion. See Rody's answer or here. One can argue that this is not what assertions are supposed to be used for, but you can certainly use them this way.
Use the validateattributesfunction. See here. This is a very good balance between simplicity and utility. It allows you to check for a number of properties in an argument (and generally, any variable at any part of code)
Use the inputParser class. See here. This is the most powerful method of parsing inputs, but may be overkill. Also, the cost of creating an inputParser object means that it may not be a good idea for functions that are called repeatedly. Nevertheless, it's very good for the public API.

Using LuaJ with Scala

I am attempting to use LuaJ with Scala. Most things work (actually all things work if you do them correctly!) but the simple task of setting object values has become incredibly complicated thanks to Scala's setter implementation.
Scala:
class TestObject {
var x: Int = 0
}
Lua:
function myTestFunction(testObject)
testObject.x = 3
end
If I execute the script or line containing this Lua function and pass a coerced instance of TestObject to myTestFunction this causes an error in LuaJ. LuaJ is trying to direct-write the value, and Scala requires you to go through the implicitly-defined setter (with the horrible name x_=, which is not valid Lua so even attempting to call that as a function makes your Lua not parse).
As I said, there are workarounds for this, such as defining your own setter or using the #BeanProperty markup. They just make code that should be easy to write much more complicated:
Lua:
function myTestFunction(testObject)
testObject.setX(testObject, 3)
end
Does anybody know of a way to get luaj to implicitly call the setter for such assignments? Or where I might look in the luaj source code to perhaps implement such a thing?
Thanks!
I must admit that I'm not too familiar with LuaJ, but the first thing that comes to my mind regarding your issue is to wrap the objects within proxy tables to ease interaction with the API. Depending upon what sort of needs you have, this solution may or may not be the best, but it could be a good temporary fix.
local mt = {}
function mt:__index(k)
return self.o[k] -- Define how your getters work here.
end
function mt:__newindex(k, v)
return self.o[k .. '_='](v) -- "object.k_=(v)"
end
local function proxy(o)
return setmetatable({o = o}, mt)
end
-- ...
function myTestFunction(testObject)
testObject = proxy(testObject)
testObject.x = 3
end
I believe this may be the least invasive way to solve your problem. As for modifying LuaJ's source code to better suit your needs, I had a quick look through the documentation and source code and found this, this, and this. My best guess says that line 71 of JavaInstance.java is where you'll find what you need to change, if Scala requires a different way of setting values.
f.set(m_instance, CoerceLuaToJava.coerce(value, f.getType()));
Perhaps you should use the method syntax:
testObject:setX(3)
Note the colon ':' instead of the dot '.' which can be hard to distinguish in some editors.
This has the same effect as the function call:
testObject.setX(testObject, 3)
but is more readable.
It can also be used to call static methods on classes:
luajava.bindClass("java.net.InetAddress"):getLocalHost():getHostName()
The part to the left of the ':' is evaluated once, so a statement such as
x = abc[d+e+f]:foo()
will be evaluated as if it were
local tmp = abc[d+e+f]
x = tmp.foo(tmp)

Purpose of (^) sign in iOS

I have seen in many iOS header's that (^) is utilized, and I have never come across the reasoning as of why that sign is being used. Would anyone might like to enlighten into this?
Thanks.
Those often indicate "blocks". See the Blocks Programming Topics.
Alternatively, if you watch the beginning of WWDC 2012 session 712, they also walk you through blocks with a touch of historical context.
It signifies a block. A block is a syntax that allows you to create a callback function, and pass it into a method as a parameter. In other languages this is similar to a closure, a lambda, or an anonymous class.
For example a parameter that lists:
void(^)(NSString *myStr)
is expecting you to pass in a block/function that returns void, and takes in an NSString pointer.
You can create a block, based on the expected parameters declared in the method, with this syntax:
^(<Parameters>) { <Body> }
For example, a method that is expecting a block parameter might look like this:
-(void)doSomething:(void(^)(NSString *myStr))theBlock;
...and could be called like this:
[self doSomething:^(NSString *myStr) { NSLog(#"The String is: %#", myStr); }];
Your block will be called back from doSomething: just like a function, using the parameter name:
-(void)doSomething:(void(^)(NSString *myStr))theBlock {
theBlock(#"Hello!");
}
...which would display:
The String is: Hello!
The ^ character is used for blocks, in particular, block parameters.
If you're asking why the character '^' for use in blocks, it's because there's relatively few characters left that:
Are available on all typical keyboards.
Could be used at all - i.e. aren't already significant in the language and would conflict.
Don't look stupid.
That actually narrows it down to only two or three, and of those '^' was chosen because, well, because.
There's probably a record of this on the llvm.org mailing lists and so forth, if you want to pore over the discussion in detail.
You could also look at the minutes from the C++11 committee meetings on lambdas, which went through basically the same process.

Using std::complex with iPhone's vDSP functions

I've been working on some vDSP code and I have come up against an annoying problem. My code is cross platform and hence uses std::complex to store its complex values.
Now I assumed that I would be able to set up an FFT as follows:
DSPSplitComplex dspsc;
dspsc.realp = &complexVector.front().real();
dspsc.imagp = &complexVector.front().imag();
And then use a stride of 2 in the appropriate vDSP_fft_* call.
However this just doesn't seem to work. I can solve the issue by doing a vDSP_ztoc but this requires temporary buffers that I really don't want hanging around. Is there any way to use the vDSP_fft_* functions directly on interleaved complex data? Also can anyone explain why I can't do as I do above with a stride of 2?
Thanks
Edit: As pointed out by Bo Persson the real and imag functions don't actually return a reference.
However it still doesn't work if I do the following instead
DSPSplitComplex dspsc;
dspsc.realp = ((float*)&complexVector.front()) + 0;
dspsc.imagp = ((float*)&complexVector.front()) + 1;
So my original question still does stand :(
The std::complex functions real() and imag() return by value, they do not return a reference to the members of complex.
This means that you cannot get their addresses this way.
This is how you do it.
const COMPLEX *in = reinterpret_cast<const COMPLEX*>(std::complex);
Source: http://www.fftw.org/doc/Complex-numbers.html
EDIT:
To clarify the source; COMPLEX and fftw_complex use the same data layout (although fftw_complex uses double and COMPLEX float)

Newbie Objective C developer question

I have been looking everywhere for an answer to this question - perhaps I'm looking in the wrong places. Also, I'm brand new to Objective C although I have around 10 years of experience as a developer.
for this code:
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
what would the method definition look like?
- (void)makeGroup:(Group *)g, (NSString *)memberOne, ...?
Thanks for any help you can provide. I know this is probably very simple...
Thanks,
R
It looks like you have a method that can take a variable number of arguments. If that's the case, the definition would look something like:
- (void)makeGroup:(Group *)g, ...;
Check out NSString's stringWithFormat: or NSArray's arrayWithObjects: methods for examples.
Edit: Upon further documentation reading, it seems that you are looking at the exact example that's in the Objective-C 2.0 documentation. The declaration you're looking for is right at the bottom of page 36.
You can receive an infinte number of arguments with an ellipsis (...). Check this for further details!
It would make more sense to have the members as a separate array argument, like -(void)makeGroup:(Group *)g members:(NSArray *)members. If you must do varargs (which is a pain), it should be written like -(void)makeGroup:(Group *)g members:(NSString *)firstMember, ....
Since I this is trying to figure out how an example method from the documentation would be declared, it would be like this:
- (void)makeGroup:(id)group, ...
Then you would start up the varags machinery with the group argument and use it to find the other arguments.
either you're looking for MrHen's answer if you're seeking to do your own class method or if you want to do them separately you write the following into your header file:
-(void)makeGroup:(Group *)g;
-(NSString *)memberOne;
EDIT: I answered the wrong question. Ignore this.
The correct way to do this is:
-(void)makeGroup:(Group *)g memberOne:(NSString *)memberOne memberTwo:(NSString *)memberTwo memberThree:(NSString *)memberThree {
...
}
The call will look like this:
[receiver makeGroup:group memberOne:memberOne memberTwo:memberTwo memberThree:memberThree];