예제 #1
0
파일: fcm.c 프로젝트: trygogins/teller
double
get_new_value(int i, int j) {
    int k;
    double t, p, sum;
    sum = 0.0;
    p = 2 / (fuzziness - 1);
    for (k = 0; k < num_clusters; k++) {
        t = get_norm(i, j) / get_norm(i, k);
        t = pow(t, p);
        sum += t;
    }
    return 1.0 / sum;
}
예제 #2
0
static int nc(void *normp) {
	const char **norm = normp;
	if (get_norm(*norm) == -1) {
		ERROR("norm \"%s\" is not supported, choose from PAL, NTSC, SECAM and auto\n", *norm);
		return 0;
	} else return 1;
}
예제 #3
0
Vector Vector::normalize(){
    float n = get_norm();
    if (n != 0) {
        return Vector(x/n, y/n);
    }else{
        return Vector(); //must be 0 0 then
    }
}
예제 #4
0
SEXP dgeMatrix_solve(SEXP a)
{
    /*  compute the 1-norm of the matrix, which is needed
	later for the computation of the reciprocal condition number. */
    double aNorm = get_norm(a, "1");

    /* the LU decomposition : */
    SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dgeMatrix"))),
	lu = dgeMatrix_LU_(a, TRUE);
    int *dims = INTEGER(GET_SLOT(lu, Matrix_DimSym)),
	*pivot = INTEGER(GET_SLOT(lu, Matrix_permSym));

    /* prepare variables for the dgetri calls */
    double *x, tmp;
    int	info, lwork = -1;


    if (dims[0] != dims[1]) error(_("Solve requires a square matrix"));
    slot_dup(val, lu, Matrix_xSym);
    x = REAL(GET_SLOT(val, Matrix_xSym));
    slot_dup(val, lu, Matrix_DimSym);

    if(dims[0]) /* the dimension is not zero */
    {
        /* is the matrix is *computationally* singular ? */
        double rcond;
        F77_CALL(dgecon)("1", dims, x, dims, &aNorm, &rcond,
                         (double *) R_alloc(4*dims[0], sizeof(double)),
                         (int *) R_alloc(dims[0], sizeof(int)), &info);
        if (info)
            error(_("error [%d] from Lapack 'dgecon()'"), info);
        if(rcond < DOUBLE_EPS)
            error(_("Lapack dgecon(): system computationally singular, reciprocal condition number = %g"),
		  rcond);

        /* only now try the inversion and check if the matrix is *exactly* singular: */
	F77_CALL(dgetri)(dims, x, dims, pivot, &tmp, &lwork, &info);
	lwork = (int) tmp;
	F77_CALL(dgetri)(dims, x, dims, pivot,
			 (double *) R_alloc((size_t) lwork, sizeof(double)),
			 &lwork, &info);
	if (info)
	    error(_("Lapack routine dgetri: system is exactly singular"));
    }
    UNPROTECT(1);
    return val;
}
예제 #5
0
static t_vec    get_cone(t_hit *hit)
{
  t_vec		n;
  t_vec		center;
  float		add;
  t_cone	*cone;

  cone = hit->obj;
  center = hit->meta.pos;
  n = get_vec(&center, &hit->rot_pt);
  add = get_norm(1, &n) / cos(cone->angle);
  if (hit->rot_pt.z > center.z)
    center.z += add;
  else
    center.z -= add;
  n = get_uni_vec(&center, &hit->rot_pt);
  rot_vec_inv(&n, &hit->meta.rot);
  return (n);
}
예제 #6
0
파일: PlanetOrbit.c 프로젝트: OxES/MyFuncs
static PyObject * get_norm_py(PyObject *self, PyObject *args, PyObject *keywds)
{
	//for numpy array
	PyObject * M_object;
	PyArrayObject * M, * norm;
	
	//planet parameter inputs
	double a_rstar,ecc,peri,incl; //Mean_anom, a/rstar, ecc, arg of periastron
	
	//PyArrayObject * output;
	int size,i;
	
	//read in python arguments
	if (!PyArg_ParseTuple(args, "Odddd", &M_object, &a_rstar, &ecc, &peri, &incl))
	    {
		printf("Problem loading args...\n");
		return NULL;
		}

	//convert numpy object to contiguous array of doubles - will accept up to 10d arrays
	M = (PyArrayObject*) PyArray_ContiguousFromObject(M_object, PyArray_DOUBLE, 1, 10);
	if(M == NULL) return NULL;
	
    //calculate no of data points in array
	size=1;
	for(i=0;i<M->nd;i++) size *= M->dimensions[i];
	
	//create output double array
	norm = (PyArrayObject*) PyArray_SimpleNew(M->nd, M->dimensions, PyArray_DOUBLE);

	//do calculation on numpy array
    for(i=0;i<size;i++){
    	*(((double*)norm->data) + i) = get_norm(*(((double*)M->data)+i),a_rstar,ecc,peri,incl);
		//printf("a: %lf %lf %lf %lf %lf\n", get_x(*(((double*)M->data)+i),a_rstar,ecc,peri),*(((double*)M->data)+i),a_rstar,ecc,peri);
		//printf("%lf\n", *(((double*)z_coord->data) + i));
		}
	
	//destroy reference to array
	Py_DECREF(M);
	
	//return array
	return PyArray_Return(norm);
}
예제 #7
0
SEXP dgeMatrix_rcond(SEXP obj, SEXP type)
{
    SEXP LU = PROTECT(dgeMatrix_LU_(obj, FALSE));/* <- not warning about singularity */
    char typnm[] = {'\0', '\0'};
    int *dims = INTEGER(GET_SLOT(LU, Matrix_DimSym)), info;
    double anorm, rcond;

    if (dims[0] != dims[1] || dims[0] < 1) {
	UNPROTECT(1);
	error(_("rcond requires a square, non-empty matrix"));
    }
    typnm[0] = La_rcond_type(CHAR(asChar(type)));
    anorm = get_norm(obj, typnm);
    F77_CALL(dgecon)(typnm,
		     dims, REAL(GET_SLOT(LU, Matrix_xSym)),
		     dims, &anorm, &rcond,
		     (double *) R_alloc(4*dims[0], sizeof(double)),
		     (int *) R_alloc(dims[0], sizeof(int)), &info);
    UNPROTECT(1);
    return ScalarReal(rcond);
}
예제 #8
0
SEXP dgeMatrix_norm(SEXP obj, SEXP type)
{
    return ScalarReal(get_norm(obj, CHAR(asChar(type))));
}
예제 #9
0
int main(int argc, char* argv[])
{
	if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
		std::cerr << "Couldn't initialize MPI." << std::endl;
		abort();
	}

	MPI_Comm comm = MPI_COMM_WORLD;

	int rank = 0, comm_size = 0;
	if (MPI_Comm_rank(comm, &rank) != MPI_SUCCESS) {
		std::cerr << "Couldn't obtain MPI rank." << std::endl;
		abort();
	}
	if (MPI_Comm_size(comm, &comm_size) != MPI_SUCCESS) {
		std::cerr << "Couldn't obtain size of MPI communicator." << std::endl;
		abort();
	}

	float zoltan_version;
	if (Zoltan_Initialize(argc, argv, &zoltan_version) != ZOLTAN_OK) {
		std::cerr << "Zoltan_Initialize failed." << std::endl;
		abort();
	}


	constexpr size_t
		nr_of_values = 100,
		max_nr_of_cells = 512;

	const unsigned int neighborhood_size = 1;
	const std::array<bool, 3> periodic = {{true, true, true}};

	dccrg::Cartesian_Geometry::Parameters geom_params;
	geom_params.start = {{-3,  -5,  -7}};
	geom_params.level_0_cell_length = {{7,   5,   3}};

	for (size_t nr_cells = 1; nr_cells <= max_nr_of_cells; nr_cells *= 2) {
		Grid grid;
		const std::array<uint64_t, 3> nr_of_cells{{nr_cells, 1, 1}};
		if (
			not grid.initialize(
				nr_of_cells, comm, "RANDOM", neighborhood_size, 0,
				periodic[0], periodic[1], periodic[2]
			)
		) {
			std::cerr << __FILE__ << ":" << __LINE__
				<< ": Couldn't initialize grids."
				<< std::endl;
			abort();
		}

		if (not grid.set_geometry(geom_params)) {
			std::cerr << __FILE__ << "(" << __LINE__ << "): "
				<< "Couldn't set geometry of grids."
				<< std::endl;
			abort();
		}

		grid.balance_load();
		grid.update_copies_of_remote_neighbors();

		const auto cell_ids = grid.get_cells();

		create_particles(nr_of_values, cell_ids, grid);

		for (const auto& cell_id: cell_ids) {
			auto* const cell_data = grid[cell_id];
			if (cell_data == nullptr) {abort();}
			bulk_value_getter(*cell_data) = 0;
		}
		pamhd::particle::accumulate(
			cell_ids,
			grid,
			[](Cell& cell)->pamhd::particle::Particles_Internal::data_type&{
				return cell[pamhd::particle::Particles_Internal()];
			},
			[](pamhd::particle::Particle_Internal& particle)
				->pamhd::particle::Position::data_type&
			{
				return particle[pamhd::particle::Position()];
			},
			[](Cell&, pamhd::particle::Particle_Internal& particle)
				->pamhd::particle::Mass::data_type&
			{
				return particle[pamhd::particle::Mass()];
			},
			bulk_value_getter,
			list_bulk_value_getter,
			list_target_getter,
			accumulation_list_length_getter,
			accumulation_list_getter
		);

		Cell::set_transfer_all(true, pamhd::particle::Nr_Accumulated_To_Cells());
		grid.update_copies_of_remote_neighbors();
		Cell::set_transfer_all(false, pamhd::particle::Nr_Accumulated_To_Cells());

		allocate_accumulation_lists(grid);

		// transfer accumulated values between processes
		Cell::set_transfer_all(true, Accumulated_To_Cells());
		grid.update_copies_of_remote_neighbors();
		Cell::set_transfer_all(false, Accumulated_To_Cells());

		accumulate_from_remote_neighbors(grid);

		// transform accumulated mass to mass density
		for (const auto& cell_id: cell_ids) {
			const auto cell_length = grid.geometry.get_length(cell_id);
			const auto volume = cell_length[0] * cell_length[1] * cell_length[2];

			auto* const cell_data = grid[cell_id];
			if (cell_data == nullptr) {
				std::cerr << __FILE__ << "(" << __LINE__ << ")" << std::endl;
				abort();
			}
			(*cell_data)[Mass_Density()] /= volume;
		}

		const double norm = get_norm(cell_ids, grid, comm);

		if (norm > 1e-10) {
			if (rank == 0) {
				std::cerr << __FILE__ << ":" << __LINE__
					<< ": Norm is too large: " << norm
					<< " with " << nr_cells << " cell(s)"
					<< std::endl;
			}
			MPI_Finalize();
			return EXIT_FAILURE;
		}
	}

	MPI_Finalize();
	return EXIT_SUCCESS;
}
예제 #10
0
파일: gl_wrapper.c 프로젝트: cloverhap/map
/***
 * Rotating
 * This is a relative rotation, from the p.o.v. of the camera
 * TODO: use quaternion representation (This is a big task)
 ****/
void rotate_eye(GLdouble h, GLdouble v) {
#ifdef DEBUG_FUNC
    static int rotate_eye_count;
    rotate_eye_count++;
    printf("Rotate count: %i\n", rotate_eye_count);
#endif
#ifdef DEBUG_VALUE
    printf("Rotate by: %lf %lf\n", h, v);
#endif

    // Calculate the player's directional axis in terms of the reference axis
    GLdouble x_axis[3] = {0,0,0};
    GLdouble y_axis[3] = {0,0,0};
    GLdouble z_axis[3] = {0,0,0};
    get_rel_axis(x_axis, y_axis, z_axis);

    // Calculate the new center point relative to the eye
    GLdouble movement[3] = {0,0,0};
    // 1. Apply horizontal rotation:
    //    Relative direction vector is (-sin(h)*x_axis, 0, -cos(h)*z_axis)
    if (h) {
        movement[0] = -sin(h)*x_axis[0] - cos(h)*z_axis[0];
        movement[1] = -sin(h)*x_axis[1] - cos(h)*z_axis[1];
        movement[2] = -sin(h)*x_axis[2] - cos(h)*z_axis[2];
        //printf("Movement after horizontal rotation: [%lf %lf %lf]\n",movement[0],movement[1],movement[2]);
    }
    // 2. Apply vertical rotation:
    //    Relative direction vector is (0, sin(v)*y_axis, -cos(v)*z_axis)
    if (v) {
        movement[0] = sin(v)*y_axis[0] - cos(v)*z_axis[0];
        movement[1] = sin(v)*y_axis[1] - cos(v)*z_axis[1];
        movement[2] = sin(v)*y_axis[2] - cos(v)*z_axis[2];
        //printf("After vertical: [%lf %lf %lf]\n",movement[0],movement[1],movement[2]);
    }
    // 3. Normalize the result and scale it to previous eye to center distance
    //    This step isn't needed, but for sake of clarity in debugging purposes only
    normalize(movement,movement);
    GLdouble f[3] = {center_pos[0] - eye_pos[0], center_pos[1] - eye_pos[1], center_pos[2] - eye_pos[2]};  // the vector pointing forward
    GLdouble scale = get_norm(f,3);
    //printf("Movement norm before scaling: %lf\n",get_norm(movement,3));
    movement[0] *= scale;
    movement[1] *= scale;
    movement[2] *= scale;
    //printf("Movement norm after scaling: %lf\n",get_norm(movement,3));
    // These steps lead to an approximate rotation

    // Note the movement of the view
    if (h || v) { // as long as there is movement
        center_pos[0] = eye_pos[0] + movement[0];
        center_pos[1] = eye_pos[1] + movement[1];
        center_pos[2] = eye_pos[2] + movement[2];
    }
    // Note the movement of up
    if (v) {
        up_pos[0] = cos(v)*y_axis[0] + sin(v)*z_axis[0];
        up_pos[1] = cos(v)*y_axis[1] + sin(v)*z_axis[1];
        up_pos[2] = cos(v)*y_axis[2] + sin(v)*z_axis[2];
        normalize(up_pos,up_pos);
    }

#ifdef DEBUG_VALUE
    printf("New center relative to the eye: [%lf %lf %lf]\n",movement[0],movement[1],movement[2]);
    //printf("Angle, rotation vector: %f, %f %f %f\n", rangle, rvector[0], rvector[1], rvector[2]);
#endif
    print_pos();

    // Apply changes
    glPushAttrib(GL_MATRIX_MODE);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(eye_pos[0],eye_pos[1],eye_pos[2],
              center_pos[0],center_pos[1],center_pos[2],
              up_pos[0],up_pos[1],up_pos[2]);
    glPopAttrib();
}
예제 #11
0
static int preinit(const char *arg) {
	vo_zr2_priv_t *p = &priv;
	const char *dev = NULL;
	char *dev_arg = NULL, *norm_arg = NULL;
	int norm = VIDEO_MODE_AUTO, prebuf = 0;
	const opt_t subopts[] = { /* don't want warnings with -Wall... */
		{ "dev",    OPT_ARG_MSTRZ, &dev_arg,   NULL 	       },
		{ "prebuf", OPT_ARG_BOOL,  &prebuf,    pbc },
		{ "norm",   OPT_ARG_MSTRZ, &norm_arg,  nc  },
		{ NULL,     0, 		   NULL,       NULL 	       }
	};

	VERBOSE("preinit() called with arg: %s\n", arg);
	memset(p, 0, sizeof(*p)); /* set defaults */
	p->vdes = -1;

	if (subopt_parse(arg, subopts)) {
		mp_msg(MSGT_VO, MSGL_FATAL,
				"Allowed suboptions for -vo zr2 are:\n"
				"-  dev=DEVICE               (default: %s)\n"
				"-  norm=PAL|NTSC|SECAM|auto (default: auto)\n"
				"-  prebuf/noprebuf          (default:"
				" noprebuf)\n"
				"\n"
				"Example: mplayer -vo zr2:dev=/dev/video1:"
				"norm=PAL movie.avi\n\n"
				, guess_device(NULL, 0));
		free(norm_arg);
		free(dev_arg);
		return -1;
	}

	/* interpret the strings we got from subopt_parse */
	if (norm_arg) {
		norm = get_norm(norm_arg);
		free(norm_arg);
	}

	if (dev_arg) dev = dev_arg;

	dev = guess_device(dev, 1);
	if (!dev) {
		free(dev_arg);
		uninit();
		return 1;
	}

	p->vdes = open(dev, O_RDWR);
	if (p->vdes < 0) {
		ERROR("error opening %s: %s\n", dev, strerror(errno));
		free(dev_arg);
		uninit();
		return 1;
	}

	free(dev_arg);

	/* check if we really are dealing with a zoran card */
	if (ioctl(p->vdes, MJPIOC_G_PARAMS, &p->zp) < 0) {
		ERROR("%s probably is not a DC10(+)/buz/lml33\n", dev);
		uninit();
		return 1;
	}

	VERBOSE("kernel driver version %d.%d, current norm is %s\n",
			p->zp.major_version, p->zp.minor_version,
			normstring(p->zp.norm));

	/* changing the norm in the zoran_params and MJPIOC_S_PARAMS
	 * does nothing the last time I tried, so bail out if the norm
	 * is not correct */
	if (norm != VIDEO_MODE_AUTO &&  p->zp.norm != norm) {
		ERROR("mplayer currently can't change the video norm, "
				"change it with (eg.) XawTV and retry.\n");
		uninit();
		return 1;
	}

	/* gather useful information */
	if (ioctl(p->vdes, VIDIOCGCAP, &p->vc) < 0) {
		ERROR("error getting video capabilities from %s\n", dev);
		uninit();
		return 1;
	}

	VERBOSE("card reports maxwidth=%d, maxheight=%d\n",
			p->vc.maxwidth, p->vc.maxheight);

	/* according to the mjpegtools source, some cards return a bogus
	 * vc.maxwidth, correct it here. If a new zoran card appears with a
	 * maxwidth different 640, 720 or 768 this code may lead to problems */
	if (p->vc.maxwidth != 640 && p->vc.maxwidth != 768) {
		VERBOSE("card probably reported bogus width (%d), "
				"changing to 720\n", p->vc.maxwidth);
		p->vc.maxwidth = 720;
	}

	p->zrq.count = ZR2_MJPEG_NBUFFERS;
	p->zrq.size = ZR2_MJPEG_SIZE;

	if (ioctl(p->vdes, MJPIOC_REQBUFS, &p->zrq)) {
		ERROR("error requesting %d buffers of size %d\n",
				ZR2_MJPEG_NBUFFERS, ZR2_MJPEG_NBUFFERS);
		uninit();
		return 1;
	}

	VERBOSE("got %ld buffers of size %ld (wanted %d buffers of size %d)\n",
			p->zrq.count, p->zrq.size, ZR2_MJPEG_NBUFFERS,
			ZR2_MJPEG_SIZE);

	p->buf = (unsigned char*)mmap(0, p->zrq.count*p->zrq.size,
			PROT_READ|PROT_WRITE, MAP_SHARED, p->vdes, 0);

	if (p->buf == MAP_FAILED) {
		ERROR("error mapping requested buffers: %s", strerror(errno));
		uninit();
		return 1;
	}

    	return 0;
}
예제 #12
0
static double	calc_wave(t_vector n, double cur_n,
			  double normal_perturb, double cur_p)
{
  return (cur_n + (cos(cur_p / normal_perturb)
		   * (get_norm(n) / normal_perturb)));
}