예제 #1
0
float64_t COctaveInterface::get_real()
{
	const octave_value f=get_arg_increment();
	if (!f.is_real_scalar())
		SG_ERROR("Expected Scalar Float as argument %d\n", m_rhs_counter);

	return f.double_value();
}
예제 #2
0
bool COctaveInterface::get_bool()
{
	const octave_value b=get_arg_increment();
	if (b.is_bool_scalar())
		return b.bool_value();
	else if (b.is_real_scalar())
		return (b.double_value()!=0);
	else
		SG_ERROR("Expected Scalar Boolean as argument %d\n", m_rhs_counter);

	return false;
}
예제 #3
0
int32_t COctaveInterface::get_int()
{
	const octave_value i=get_arg_increment();
	if (!i.is_real_scalar())
		SG_ERROR("Expected Scalar Integer as argument %d\n", m_rhs_counter);

	double s=i.double_value();
	if (s-CMath::floor(s)!=0)
		SG_ERROR("Expected Integer as argument %d\n", m_rhs_counter);

	return int32_t(s);
}
예제 #4
0
input_test_val type_test(const octave_value&  to_be_test, data_type reference_type )
{   
    input_test_val A = input_test_val();
    A.value = true;
    switch( reference_type )
    {
        case real_scalar:
             A.value = !to_be_test.is_real_scalar(); 
             if(A.value)      
                 A.error_code="Argument isn't real scalar";
        break;
        case complex_scalar:
             A.value = !(to_be_test.is_complex_scalar() );
             if(A.value) 
                 A.error_code="Argument isn't complex scalar";
        break;
        case complex_array:
	     A.value = !(to_be_test.is_complex_type() && (to_be_test.rows() > 1 || to_be_test.columns() > 1) );
             if(A.value)
                 A.error_code="Argument isn't complex array";
        break;
        case real_array:  
             A.value = !(to_be_test.is_real_type() && (to_be_test.rows() > 1 || to_be_test.columns() > 1) );
             if(A.value)
                 A.error_code="Argument isn't real array";
        break;
    }

    return A;
}
예제 #5
0
/*--------------------------------------------------------------------------------*/
double get_value(octave_value val)
{
  NDArray myptr=val.array_value();
  double myval=(double)myptr(0,0);
  return myval;

}
예제 #6
0
actData get_value(octave_value val)
{
  NDArray myptr=val.array_value();
  actData myval=(actData)myptr(0,0);
  return myval;

}
예제 #7
0
char* COctaveInterface::get_string(int32_t& len)
{
	const octave_value s=get_arg_increment();
	if (!s.is_string())
		SG_ERROR("Expected String as argument %d\n", m_rhs_counter);

	std::string std_str=s.string_value();
	const char* str= std_str.c_str();
	len=std_str.length();
	ASSERT(str && len>0);

	char* cstr=new char[len+1];
	memcpy(cstr, str, len+1);
	cstr[len]='\0';

	return cstr;
}
예제 #8
0
void COctaveInterface::get_real_sparsematrix(SGSparseVector<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)
{
	const octave_value mat_feat=get_arg_increment();
	if (!mat_feat.is_sparse_type() || !(mat_feat.is_double_type()))
		SG_ERROR("Expected Sparse Double Matrix as argument %d\n", m_rhs_counter);

	SparseMatrix sm = mat_feat.sparse_matrix_value ();
	num_vec=sm.cols();
	num_feat=sm.rows();
	int64_t nnz=sm.nelem();

	matrix=new SGSparseVector<float64_t>[num_vec];

	int64_t offset=0;
	for (int32_t i=0; i<num_vec; i++)
	{
		int32_t len=sm.cidx(i+1)-sm.cidx(i);
		matrix[i].vec_index=i;
		matrix[i].num_feat_entries=len;

		if (len>0)
		{
			matrix[i].features=new SGSparseVectorEntry<float64_t>[len];

			for (int32_t j=0; j<len; j++)
			{
				matrix[i].features[j].entry=sm.data(offset);
				matrix[i].features[j].feat_index=sm.ridx(offset);
				offset++;
			}
		}
		else
			matrix[i].features=NULL;
	}
	ASSERT(offset=nnz);
}
예제 #9
0
/**
 * Converts an Octave object into an R object.
 */
template <> SEXP Rcpp::wrap( const octave_value& val){

	VERBOSE_LOG("wrap<%s>", val.type_name().c_str());
	if( val.is_null_value() ){

		VERBOSE_LOG("null_value");
		return R_NilValue;

	}else if (val.is_matrix_type()) {// matrix value: row vectors are converted into R vectors

		// check if multidimensional array
		if( val.ndims() > 2 ){
			VERBOSE_LOG("(NDArray) -> Array");
			return ::wrap(val.array_value());
		}else if ( val.is_string() ){

			VERBOSE_LOG("(CellStr) -> CharacterVector");
			//const string_vector s(val.cellstr_value()); // works >= 3.4.3
			const Cell& s = val.cellstr_value();
			int n = s.length();
			if( n == 0 )
				return CharacterVector(val.string_value());

			// character vector
			SEXP res = wrap(s);
			VERBOSE_LOG("[%i]\n", Rf_length(res));
			return res;

		}else if ( val.is_char_matrix() ){
			VERBOSE_LOG("(charMatrix) -> CharacterVector");
			charMatrix m = val.char_matrix_value();
			int n = m.rows();
			CharacterVector res(n);
			for(int i=0; i<n; ++i)
				res[i] = m.row_as_string(i);
			return res;
		}
		else if ( val.is_bool_type() ){

			VERBOSE_LOG("(boolMatrix) -> LogicalMatrix");
			return wrapArray<LGLSXP>(val.bool_matrix_value());

		}else if( val.is_int32_type() || val.is_int64_type() || val.is_int16_type() || val.is_integer_type() ){

			return ::wrap(static_cast<oct_intArray>(val.int32_array_value()));

		}else if( val.is_real_type() ){
			return ::wrap(val.matrix_value());
		}else{

			std::ostringstream err;
			err << " - Octave matrix type `" << val.type_name().c_str() << "` not supported.";
			WRAP_ERROR(err.str().c_str());

		}

		return R_NilValue;

	}
	else if (val.is_string()) {// single character string

		VERBOSE_LOG("(string)\n");
		const std::string s(val.string_value());
		return CharacterVector(s);

	}else if (val.is_scalar_type()) {// single scalar value

		if ( val.is_bool_scalar() ){

			VERBOSE_LOG("(bool_value)\n");
			return wrap(val.bool_value());

		}
		else if ( val.is_integer_type() ){

			VERBOSE_LOG("(int_value)\n");
			return wrap(val.int_value());

		}else if( val.is_real_type() ){

			VERBOSE_LOG("(double_value)\n");
			return wrap(val.double_value());

		}else{

			std::ostringstream err;
			err << " - Octave scalar type `" << val.type_name().c_str() << "` not supported.";
			WRAP_ERROR(err.str().c_str());

		}

		return R_NilValue;

	} else if( val.is_map() ){ // Maps are converted into lists

		VERBOSE_LOG("(map) -> ");

		OCTAVE_MAP m = val.map_value();
		const string_vector& keys = m.keys();
		int n = keys.length();
		Rcpp::List res;
		if (keys.length () == 0){
			VERBOSE_LOG("List[0] (no names)\n");
			return res;
		}else{
			VERBOSE_LOG("NamedList[%i]:\n", n);
			int nempty = 0;
			for(int i=0; i < n; ++i){
				const string& k = keys[i];
				VERBOSE_LOG("$'%s'\t: ", k.c_str());
				if( k[0] == '\0' ){
					if( ++nempty > 1 )
						WRAP_ERROR("<NamedList> - More than one empty name in Octave map");
				}
				const Cell& cell = m.contents(k);
				if( cell.length() == 0 ){
					VERBOSE_LOG("empty\n");
					res[k] = R_NilValue;
					continue;
				}
				res[k] = ::wrap(cell);
			}
			return res;
		}

	} else if( val.is_cs_list() PRE_3_4_0(|| val.is_list()) ){

		VERBOSE_LOG("(cs_list) => List\n");
		return wrap<octave_value_list>(val.list_value());

	} else if( val.is_cell() ){// Cell objects are used for character vectors
예제 #10
0
파일: Utils.cpp 프로젝트: Htallon/QtHandles
QImage makeImageFromCData (const octave_value& v, int width, int height)
{
  dim_vector dv (v.dims ());

  if (dv.length () == 3 && dv(2) == 3)
    {
      int w = qMin (dv(1), width);
      int h = qMin (dv(0), height);

      int x_off = (w < width ? (width - w) / 2 : 0);
      int y_off = (h < height ? (height - h) / 2 : 0);

      QImage img (width, height, QImage::Format_ARGB32);
      img.fill (qRgba (0, 0, 0, 0));

      if (v.is_uint8_type ())
	{
	  uint8NDArray d = v.uint8_array_value ();

	  for (int i = 0; i < w; i++)
	    for (int j = 0; j < h; j++)
	      {
		int r = d(j, i, 0);
		int g = d(j, i, 1);
		int b = d(j, i, 2);
		int a = 255;

		img.setPixel (x_off + i, y_off + j, qRgba (r, g, b, a));
	      }
	}
      else if (v.is_single_type ())
	{
	  FloatNDArray f = v.float_array_value ();

	  for (int i = 0; i < w; i++)
	    for (int j = 0; j < h; j++)
	      {
		float r = f(j, i, 0);
		float g = f(j, i, 1);
		float b = f(j, i, 2);
		int a = (xisnan (r) || xisnan (g) || xisnan (b) ? 0 : 255);

		img.setPixel (x_off + i, y_off + j,
			      qRgba (xround (r * 255),
				     xround (g * 255),
				     xround (b * 255),
				     a));
	      }
	}
      else if (v.is_real_type ())
	{
	  NDArray d = v.array_value ();

	  for (int i = 0; i < w; i++)
	    for (int j = 0; j < h; j++)
	      {
		double r = d(j, i, 0);
		double g = d(j, i, 1);
		double b = d(j, i, 2);
		int a = (xisnan (r) || xisnan (g) || xisnan (b) ? 0 : 255);

		img.setPixel (x_off + i, y_off + j,
			      qRgba (xround (r * 255),
				     xround (g * 255),
				     xround (b * 255),
				     a));
	      }
	}

      return img;
    }

  return QImage ();
}