I'm struggling with using dger() correctly in C. My code is as follows:
#include <stdio.h>
#include "blas.h"
int main() {
double a[4*5] = { 1, 2, 3, 4, 5,
6, 7, 8, 9,10,
11,12,13,14,15,
16,17,18,19,20
};
double x[4] = {2,3,4,5};
double y[5] = {7,8,9,10,11};
int tm=4, tn=5, tone=1;
dger(&tm, &tn, &tone, x, &tone, y, &tone, a, &tm);
}
The code compiles without error but when I execute the code, it crashes. I don't really get more details why the code crashes because I'm writing a mex C file (MATLAB C-code) - I have omitted the overhead of the mex entry function and so on.
You have to check your arguments: https://www.netlib.org/lapack/explore-html/d7/d15/group__double__blas__level2_ga458222e01b4d348e9b52b9343d52f828.html
In particular you are missing the alpha parameter (passing &tone instead).
Related
I am new to ObjC and I am using FSCalendar framework for multiple date selection. When I run this project in iOS , everything works fine but when I do this as an FFI for kony , I am getting too many errors like the following. Can anyone help me to resolve these bugs?
#import "FSCalendarConstants.h"
#class FSCalendar;
typedef NS_ENUM(NSInteger, FSCalendarCellState) {
FSCalendarCellStateNormal = 0,
FSCalendarCellStateSelected = 1,
FSCalendarCellStatePlaceholder = 1 << 1,
FSCalendarCellStateDisabled = 1 << 2,
FSCalendarCellStateToday = 1 << 3,
FSCalendarCellStateWeekend = 1 << 4,
FSCalendarCellStateTodaySelected = FSCalendarCellStateToday|FSCalendarCellStateSelected
};
typedef NS_ENUM(NSUInteger, FSCalendarSeparators) {
FSCalendarSeparatorNone = 0,
FSCalendarSeparatorInterRows = 1
};
I'm working on an application and my problem started when i tried to encode a model that has an enum property using NSCoding. So i had the idea to convert it to the rawValue and the way back. I looked around a bit and came around the macro NS_ENUM, so my code looks like this:
typedef NS_ENUM(NSInteger, SectionType) {
SectionTypeText = 0,
SectionTypeVideo = 1,
SectionTypeLink = 2,
SectionTypeFile = 3,
SectionTypeQuiz = 4,
SectionTypeAudio = 5,
SectionTypeGame = 6,
SectionTypeHomework = 7
};
But i could find no possible way to convert these to the associated value and the way back. How could i do it? There is a better approach than the NS_ENUM macro?
My Objective-C is a bit rusty, but I think I would just cast it:
SectionType type = (SectionType) 2;
Back works the same:
int typeNumber = type;
How would you write a variadic macro that can take either 1 or 0 arguments. I.e. something like this:
GREET() // returns #"Hello World"
GREET(#"John") // returns #"Hello John"
It's quite simple, you have something like this:
#define __NARGS(unused, _1, _2, _3, _4, _5, VAL, ...) VAL
#define NARGS(...) __NARGS(unused, ## __VA_ARGS__, 5, 4, 3, 2, 1, 0)
#define __GREET(ARGC, ARGS...) GREET_ ## ARGC (ARGS)
#define _GREET(ARGC, ARGS...) __GREET(ARGC, ARGS)
#define GREET(...) _GREET(NARGS(__VA_ARGS__), __VA_ARGS__)
#define GREET_0(...) #"Hello World!"
#define GREET_1(ARG, ...) #"Hello, " ARG // strings are auto-concatenated in objc
int main()
{
NSLog(#"%#", GREET());
NSLog(#"%#", GREET(#"John"));
}
Output:
2012-09-30 11:56:48.478 TestProj[51823:303] Hello World!
2012-09-30 11:56:48.480 TestProj[51823:303] Hello, John
Now, this is quite complex, but assuming you understand at a basic level how the preprocessor works, you should be in a good position to understand what is happening.
I don't know if this would work for objective C, but for C99 and C11 you can use P99 that has a meta macro P99_IF_EMPTY
#define GREET(...) P99_IF_EMPTY(__VA_ARGS__)("Hello World")("Hello " __VA_ARGS__)
A good way to do this is to build a data structure with a repeating element, such as:
union greet_arg {
char *string;
};
struct greet_args {
union greet_arg *arg[2];
};
void greet_function(struct greet_args *x);
Your macro can then be implemented like this:
#define GREET(x...) greet_function(&(struct greet_args){0, x})
Now the reason this works is that if you call GREET("foo") then you get:
greet_function(&(struct greet_args){0, "foo"});
whereas if you call GREET() you get:
greet_function(&(struct greet_args){0, });
which is still valid; the "0" simply null-fills the rest of the array.
Your greet_function() then simply check x->arg[1].
Either a macro has variadic arguments, or it has a fixed number of arguments. To get the desired result, declare 2 macros, one with 0 parameters and one with 1 parameter.
I'm trying to pass a two-dimensional array, which size can be dynamic, as a method argument.
Within the method I'd like to use the array with the general array syntax.
int item = array[row][column];
To pass the array is not possible, so I thought about to use a pointer pointer.
- (void)doSomethingWithArray:(int **)array columns:(int)nColumns rows:(int)nRows
{
int item = array[n][m];
}
But I get the problem when I try to pass the array as the parameter
int array[numberOfRows][numberOfColumns];
[someObject doSomethingWithArray:array columns:numberOfColumns rows:numberOfRows];
I found a lot of tips & tricks, but somehow nothing really works in the way I would like to use it.
Thanks for help,
Eny
Is objective-c based on C99?
If it is, you can use the "new" syntax that allows you to pass dimension information directly.
#include <stdio.h>
void foo(int rows, int cols, int arr[rows][cols]) {
printf("%d, %d\n", arr[0][0], arr[1][4]);
}
int main(void) {
int arr[2][12] = {{1, 2, 3, 4, 5}, {11, 12, 13, 14, 15}};
foo(2, 12, arr);
}
You can see the code running on ideone.
- (void)doSomethingWithArray:(void *)array columns:(int)nColumns rows:(int)nRows {}
...
[someObject doSomethingWithArray:&array columns:numberOfColumns rows:numberOfRows];
I want to use the GSL for integration
http://www.gnu.org/software/gsl/manual/html_node/Numerical-Integration.html
However, I find no convenient way how the integrated function
(the function f in the example http://www.gnu.org/software/gsl/manual/html_node/Numerical-integration-examples.html)
can report an error to the integrator. I want to integrate a function which itself results from an integration that could fail. This is my sample program
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>
double f (double x, void * params) {
GSL_ERROR("test error",GSL_FAILURE);
return 0.0;
}
int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function F;
F.function = &f;
gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
w, &result, &error);
printf ("status = %d\n", status);
status = GSL_FAILURE;
printf ("status = %d\n", status);
gsl_integration_workspace_free (w);
return 0;
}
resulting in the output
status = 0
status = -1
I think the integrator should rather stop and return my error code. How can I achieve this?
Thank you very much for your help!!!
2011-04-27: I also tried this variant, after Brian Gough told me,
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>
double f (double x, void * params) {
GSL_ERROR("test error",GSL_FAILURE);
return GSL_NAN;
}
int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function F;
F.function = &f;
gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
w, &result, &error);
printf ("status = %d\n", status);
status = GSL_FAILURE;
printf ("status = %d\n", status);
gsl_integration_workspace_free (w);
return 0;
}
it did not help either. I will now fill out a bug report.
Thanks to Xuebin Wu from the GSL Mailing list the problem is solved:
Hi,
GSL_ERROR itself is a macro, it looks like
gsl_error (reason, __FILE__, __LINE__, gsl_errno);
return gsl_errno;
The function already returns before you return NAN, because GSL_ERROR
has been called. Turning the handler off just let the first line do
nothing. The default error handler abort the program after printing
error message.
I do not think it is a bug. Maybe you can write your own error handler
to solve your problem. For example, you can use "goto" to jump out of
gsl_integration_qags, or set some global variable to indicate the
integration result is incorrect.
PS: I believe this macro is what you need,
Macro: GSL_ERROR_VAL (reason, gsl_errno, value)
This macro is the same as GSL_ERROR but returns a user-defined value
of value instead of an error code. It can be used for mathematical
functions that return a floating point value.
The following example shows how to return a NaN at a mathematical
singularity using the GSL_ERROR_VAL macro,
if (x == 0)
{
GSL_ERROR_VAL("argument lies on singularity",
GSL_ERANGE, GSL_NAN);
}
So I adjusted the code according to
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>
double f (double x, void * params) {
// return GSL_NAN;
GSL_ERROR_VAL ("argument lies on singularity", GSL_ERANGE, GSL_NAN);
}
int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function F;
F.function = &f;
gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
w, &result, &error);
printf ("status = %d\n", status);
status = GSL_FAILURE;
printf ("status = %d\n", status);
gsl_integration_workspace_free (w);
return 0;
}
and everything works as expected...
A bit hackish, but I'd probably have your function store some flag. When it encounters an error it sets the flag and returns zero for all subsequent evaluations. Then, after you've integrated it you can check this flag to see if the result is valid.
What about to write a wrapper for the function which returns pointer to a structure, containing function results and error status ? Or if you use c++, this encapsulation can be made with use of objects ....