Search Results

Search found 3365 results on 135 pages for 'math'.

Page 11/135 | < Previous Page | 7 8 9 10 11 12 13 14 15 16 17 18  | Next Page >

  • What is the best way to attach extenstion methods to static classes rather than to instances of a cl

    - by John Gietzen
    If I have a method for calculating the greatest common divisor of two integers as: public static int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } What would be the best way to attach that to the System.Math class? Here are the three ways I have come up with: public static int GCD(this int a, int b) { return b == 0 ? a : b.GCD(a % b); } // Lame... var gcd = a.GCD(b); and: public static class RationalMath { public static int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } } // Lame... var gcd = RationalMath.GCD(a, b); and: public static int GCD(this Type math, int a, int b) { return b == 0 ? a : typeof(Math).GCD(b, a % b); } // Neat? var gcd = typeof(Math).GCD(a, b); The desired syntax is Math.GCD since that is the standard for all mathematical functions. Any suggestions? What should I do to get the desired syntax?

    Read the article

  • What is the best way to attach static methods to classes rather than to instances of a class?

    - by John Gietzen
    If I have a method for calculating the greatest common divisor of two integers as: public static int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } What would be the best way to attach that to the System.Math class? Here are the three ways I have come up with: public static int GCD(this int a, int b) { return b == 0 ? a : b.GCD(a % b); } // Lame... var gcd = a.GCD(b); and: public static class RationalMath { public static int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); } } // Lame... var gcd = RationalMath.GCD(a, b); and: public static int GCD(this Type math, int a, int b) { return b == 0 ? a : typeof(Math).GCD(b, a % b); } // Neat? var gcd = typeof(Math).GCD(a, b); The desired syntax is Math.GCD since that is the standard for all mathematical functions. Any suggestions? What should I do to get the desired syntax?

    Read the article

  • Replacing latex with unicode symbols

    - by Elazar Leibovich
    Often, during a conversation or an email, or at a forum, I would like to type some math, but I don't need full equation support. Unicode symbols should suffice. What I need is an easy way to type math related unicode symbols. Since I already know latex, it makes sense to use the latex symbol mnemonics to type the math symbols. What I currently did is to write an AutoHotKey script which automatically replaces \latexSymbol with the corresponding unicode symbol, using the "hotstrings" AutoHotKey feautres. However, the AutoHotKey hotstrings proved unstable for many strings. Having a couple of tens lines would cause AHK to fail recognizing the strings from time to time. Any other solution? (No, Alt+unicode number isn't convenient enough) Attached is my AHK script. The PutUni function is taken from here. ::\infty:: PutUni("e2889e") return ::\sum:: PutUni("e28891") return ::\int:: PutUni("e288ab") return ::\pm:: PutUni("c2b1") return ::\alpha:: PutUni("c991") return ::\beta:: PutUni("c992") return ::\phi:: PutUni("c9b8") return ::\delta:: PutUni("ceb4") return ::\pi:: PutUni("cf80") return ::\omega:: PutUni("cf89") return ::\in:: PutUni("e28888") return ::\notin:: PutUni("e28889") return ::\iff:: PutUni("e28794") return ::\leq:: PutUni("e289a4") return ::\geq:: PutUni("e289a5") return ::\sqrt:: PutUni("e2889a") return ::\neq:: PutUni("e289a0") return ::\subset:: PutUni("e28a82") return ::\nsubset:: PutUni("e28a84") return ::\nsubseteq:: PutUni("e28a88") return ::\subseteq:: PutUni("e28a86") return ::\prod:: PutUni("e2888f") return ::\N:: PutUni("e28495") return

    Read the article

  • latex: curly braces outside math

    - by basweber
    Hi, I am producing some latex beamer slides (but I think it is not a beamer specific question per se). I have the following: \begin{itemize} \item Issue1 \item Issue2 \item Issue3 \end{itemize} now I want to have a right curly brace (i.e. '}') behind the items spreading over issue1 and issue2. And of course I want to write something behind that curly brace I a perfect world I would write something like: \begin{itemize} \left . \item Issue1 \item Issue2 \right \\} One and Two are cool \item Issue3 \end{itemize} This does not work because I am not in a math environment and I can not put the whole snippet inside a math environment because itemize would not work in that case. Is their a clean solution or a hack to produce my desired result? Regards, Bastian.

    Read the article

  • Latex: stretchable curly braces outside math

    - by basweber
    Hi, I am producing some latex beamer slides (but I think it is not a beamer specific question per se). I have the following: \begin{itemize} \item Issue1 \item Issue2 \item Issue3 \end{itemize} Now, I want to have a right curly brace (i.e. '}') behind the items spreading over issue1 and issue2. And of course I want to write something behind that curly brace. In a perfect world I would write something like: \begin{itemize} \left . \item Issue1 \item Issue2 \right \} One and Two are cool \item Issue3 \end{itemize} This does not work because I am not in a math environment and I can not put the whole snippet inside a math environment because itemize would not work in that case. Is there a clean solution or a hack to produce my desired result? Regards, Bastian.

    Read the article

  • how random is Math.random() in java across different jvms or different machines

    - by user881480
    I have a large distributed program across many different physical servers, each program spawns many threads, each thread use Math.random() in its operations to draw a piece from many common resource pools. The goal is to utilize the pools evenly across all operations. Sometimes, it doesn't appear so random by looking at a snapshot on a resource pool to see which pieces it's getting at that instant (it might actually be, but it's hard to measure and find out for sure). Is there something that's better than Math.random() and performs just as good (not much worse at least)?

    Read the article

  • How to get Distance Kilometer in android?

    - by user1787493
    i am very new to Google maps i want calculate the distance between two places in android .for that i get the two places lat and lag positions for that i write the following code: private double getDistance(double lat1, double lat2, double lon1, double lon2) { double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lon2 - lon1); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double temp = 6371 * c; temp=temp*0.621; return temp; } the above code cant give the accurate distance between two places .what is the another way to find distance please give me any suggestions thanks in advance....

    Read the article

  • round number in JavaScript to N decimal places

    - by Richard
    in JavaScript, the typical way to round a number to N decimal places is something like: function round_number(num, dec) { return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec); } However this approach will round to a maximum of N decimal places while I want to always round to N decimal places. For example "2.0" would be rounded to "2". Any ideas?

    Read the article

  • Is a line formed by two points greater than 90 degrees off of the horzontal.

    - by Scott Chamberlain
    I am trying to find out if a line defined by two points is greater than or equal to 90 degrees compared to the horizontal. Here is the code I used bool moreThan90 = false; double angle = Math.Atan((double)(EndingLocation.Y - Location.Y) / (double)(EndingLocation.X - Location.X)); if (angle >= Math.PI / 2.0 || angle <= -Math.PI / 2.0) moreThan90 = true; Did I do this correctly or is there a better built in function in .Net that will find this?

    Read the article

  • Creating ground in a 2D runner game

    - by user739711
    It may be a repetitive uestion but I could not find any specific answer to my query How to create A slanted/curved ground in a 2d runner game. The user will see side view like the old game "Mario" If I use tiled based map I can have only rectangular objects. What is the best way to create tilted ground? Should I use tiled based map, or should I define corner points in the map and create the ground programatically? And what are the difficulties in creating curved ground.

    Read the article

  • Quick 2D sight area calculation algorithm?

    - by Rogach
    I have a matrix of tiles, on some of that tiles there are objects. I want to calculate which tiles are visible to player, and which are not, and I need to do it quite efficiently (so it would compute fast enough even when I have a big matrices (100x100) and lots of objects). I tried to do it with Besenham's algorithm, but it was slow. Also, it gave me some errors: ----XXX- ----X**- ----XXX- -@------ -@------ -@------ ----XXX- ----X**- ----XXX- (raw version) (Besenham) (correct, since tunnel walls are still visible at distance) (@ is the player, X is obstacle, * is invisible, - is visible) I'm sure this can be done - after all, we have NetHack, Zangband, and they all dealt with this problem somehow :) What algorithm can you recommend for this? EDIT: Definition of visible (in my opinion): tile is visible when at least a part (e.g. corner) of the tile can be connected to center of player tile with a straight line which does not intersect any of obstacles.

    Read the article

  • Determine arc-length of a Catmull-Rom spline

    - by Wouter
    I have a path that is defined by a concatenation of Catmull-Rom splines. I use the static method Vector2.CatmullRom in XNA that allows for interpolation between points with a value going from 0 to 1. Not every spline in this path has the same length. This causes speed differences if I let the weight go at a constant speed for every spline while proceeding along the path. I can remedy this by letting the speed of the weight be dependent on the length of the spline. How can I determine the length of such a spline? Should I just approximate by cutting the spline into 10 straight lines and sum their lengths? I'm using this for dynamic texture mapping on a generated mesh defined by splines.

    Read the article

  • Approximating walking physics via simpler sliding physics

    - by Dave
    I am modeling walking insects. I implement them as cuboids and use forces (including friction and drag), to control motion. However, the movement characteristics of this 'sliding box' physics don't match those due to a legged creature. For example, legged creatures near-instantly accelerate to their top speed; whereas applying a force to a box takes time to accelerate it. The applied force can be increased along with the counteracting drag, giving much quicker acceleration (via force) to a max speed (via drag). However, this also means the force that creatures can exert when pushing on other objects is increased. Does anyone know of any techniques using a physics engine to cheaply model walking creatures?

    Read the article

  • The Numerical ‘Magic’ of Cyclic Numbers

    - by Akemi Iwaya
    If you love crunching numbers or are just a fan of awesome number ‘tricks’ to impress your friends with, then you will definitely want to have a look at cyclic numbers. Dr Tony Padilla from the University of Nottingham shows how these awesome numbers work in Numberphile’s latest video. Cyclic Numbers – Numberphile [YouTube] Want to learn more about cyclic numbers? Then make sure to visit the Wikipedia page linked below! Cyclic number [Wikipedia]     

    Read the article

  • How could you parallelise a 2D boids simulation

    - by Sycren
    How could you program a 2D boids simulation in such a way that it could use processing power from different sources (clusters, gpu). In the above example, the non-coloured particles move around until they cluster (yellow) and stop moving. The problem is that all the entities could potentially interact with each other although an entity in the top left is unlikely to interact with one in the bottom right. If the domain was split into different segments, it may speed the whole thing up, But if an entity wanted to cross into another segment there may be problems. At the moment this simulation works with 5000 entities with a good frame rate, I would like to try this with millions if possible. Would it be possible to use quad trees to further optimise this? Any other suggestions?

    Read the article

  • Follow point of interest by applying torque

    - by azymm
    Given a body with an orientation angle and a point of interest or targetAngle, is there an elegant solution for keeping the body oriented towards the point of interest by applying torque or impulses? I have a naive solution working below, but the effect is pretty 'wobbly', it'll overshoot each time, slowly getting closer to the target angle - undesirable effect in my case. I'd like to find a solution that is more intelligent - that can accelerate to near the target angle then decelerate and stop right at the target angle (or within a small range). If it helps, I'm using box2d and the body is a rectangle. def gameloop(dt): targetAngle = get_target_angle() bodyAngle = get_body_angle() deltaAngle = targetAngle - bodyAngle if deltaAngle > PI: deltaAngle = targetAngle - (bodyAngle + 2.0 * PI) if deltaAngle < -PI: deltaAngle = targetAngle - (bodyAngle - 2.0 * PI) # multiply by 2, for stronger reaction deltaAngle = deltaAngle * 2.0; body.apply_torque(deltaAngle); One other thing, when body has no linear velocity, the above solution works ok. But when the body has some linear velocity, the solution above causes really wonky movement. Not sure why, but would appreciate any hints as to why that might be.

    Read the article

  • Python — Time complexity of built-in functions versus manually-built functions in finite fields

    - by stackuser
    Generally, I'm wondering about the advantages versus disadvantages of using the built-in arithmetic functions versus rolling your own in Python. Specifically, I'm taking in GF(2) finite field polynomials in string format, converting to base 2 values, performing arithmetic, then output back into polynomials as string format. So a small example of this is in multiplication: Rolling my own: def multiply(a,b): bitsa = reversed("{0:b}".format(a)) g = [(b<<i)*int(bit) for i,bit in enumerate(bitsa)] return reduce(lambda x,y: x+y,g) Versus the built-in: def multiply(a,b): # a,b are GF(2) polynomials in binary form .... return a*b #returns product of 2 polynomials in gf2 Currently, operations like multiplicative inverse (with for example 20 bit exponents) take a long time to run in my program as it's using all of Python's built-in mathematical operations like // floor division and % modulus, etc. as opposed to making my own division, remainder, etc. I'm wondering how much of a gain in efficiency and performance I can get by building these manually (as shown above). I realize the gains are dependent on how well the manual versions are built, that's not the question. I'd like to find out 'basically' how much advantage there is over the built-in's. So for instance, if multiplication (as in the example above) is well-suited for base 10 (decimal) arithmetic but has to jump through more hoops to change bases to binary and then even more hoops in operating (so it's lower efficiency), that's what I'm wondering. Like, I'm wondering if it's possible to bring the time down significantly by building them myself in ways that maybe some professionals here have already come across.

    Read the article

  • I love programming but never get projects started

    - by 0xHenry
    I read this question and the most voted answer and said to myself "Those things are what I want to do! But why the heck I'm not doing it?" The problem here is that I can't get anything started. I hope you get some kind of idea what I'm interested in and can help me continue programming. I'm interested in embedded programming and I'd like to learn some ARM assembly. I think those could be combined with image processing. Also I'd love to create programs that are actually useful to some people but it seems like there are already multiple programs for pretty much every purpose. Maybe if I found an interesting program without open source alternatives I could get a great project to work on. How could I get myself to finally start a project or few and keep myself interested in them? I just want to get myself to do something I love.

    Read the article

  • How Big Is a Billion? [Video]

    - by Jason Fitzpatrick
    A billion is a billion except, when it isn’t. Depending on where and when you were raised and educated, the world “billion” is some magnitudes different–read on to see the difference between a billion in long and short number systems. [via Geeks Are Sexy] Here’s How to Download Windows 8 Release Preview Right Now HTG Explains: Why Linux Doesn’t Need Defragmenting How to Convert News Feeds to Ebooks with Calibre

    Read the article

  • LibGDX Boid Seek Behaviour

    - by childonline
    I'm trying to make a swarm of boids which seek out the mouse position and move towards it, but I'm having a bit of a problem. The boids just seem to want to go to upper-right corner of the game window. The mouse position seems influence the behavior a bit, but not enough to make the boid turn towards it. I suspect there is a problem with the way LibGDX handles its coordinate system, but I'm not sure how to fix it I've uploaded the eclipse project here! Also here are the relevant bits of my code, in case you see something obviously wrong: public Agent(){ _texture = GdxGame.TEX_AGENT; TextureRegion region = new TextureRegion(_texture, 0, 0, 32, 32); TextureRegion region2 = new TextureRegion(GdxGame.TEX_TARGET, 0, 0, 32, 32); _sprite = new Sprite(region); _sprite.setSize(.05f, .05f); _sprite_target = new Sprite(region2); _sprite_target.setSize(.1f, .1f); _max_velocity = 0.05f; _max_speed = 0.005f; _velocity = new Vector2(0, 0); _desired_velocity = new Vector2(0, 0); _steering = new Vector2(0, 0); _position = new Vector2(-_sprite.getWidth()/2, -_sprite.getHeight()/2); _mass = 10f; } public void Update(float deltaTime){ _target = new Vector2(Gdx.input.getX(), Gdx.input.getY()); _desired_velocity = ((_target.sub(_position)).nor()).scl(_max_velocity,_max_velocity); _steering = ((_desired_velocity.sub(_velocity)).limit(_max_speed)).div(_mass); _velocity = (_velocity.add(_steering)).limit(_max_speed); _position = _position.add(_velocity); _sprite.setPosition(_position.x, _position.y); _sprite_target.setPosition(Gdx.input.getX(), Gdx.input.getY()); } I've used this tutorial here. Thanks!

    Read the article

  • Atmospheric Scattering

    - by Lawrence Kok
    I'm trying to implement atmospheric scattering based on Sean O`Neil algorithm that was published in GPU Gems 2. But I have some trouble getting the shader to work. My latest attempts resulted in: http://img253.imageshack.us/g/scattering01.png/ I've downloaded sample code of O`Neil from: http://http.download.nvidia.com/developer/GPU_Gems_2/CD/Index.html. Made minor adjustments to the shader 'SkyFromAtmosphere' that would allow it to run in AMD RenderMonkey. In the images it is see-able a form of banding occurs, getting an blueish tone. However it is only applied to one half of the sphere, the other half is completely black. Also the banding appears to occur at Zenith instead of Horizon, and for a reason I managed to get pac-man shape. I would appreciate it if somebody could show me what I'm doing wrong. Vertex Shader: uniform mat4 matView; uniform vec4 view_position; uniform vec3 v3LightPos; const int nSamples = 3; const float fSamples = 3.0; const vec3 Wavelength = vec3(0.650,0.570,0.475); const vec3 v3InvWavelength = 1.0f / vec3( Wavelength.x * Wavelength.x * Wavelength.x * Wavelength.x, Wavelength.y * Wavelength.y * Wavelength.y * Wavelength.y, Wavelength.z * Wavelength.z * Wavelength.z * Wavelength.z); const float fInnerRadius = 10; const float fOuterRadius = fInnerRadius * 1.025; const float fInnerRadius2 = fInnerRadius * fInnerRadius; const float fOuterRadius2 = fOuterRadius * fOuterRadius; const float fScale = 1.0 / (fOuterRadius - fInnerRadius); const float fScaleDepth = 0.25; const float fScaleOverScaleDepth = fScale / fScaleDepth; const vec3 v3CameraPos = vec3(0.0, fInnerRadius * 1.015, 0.0); const float fCameraHeight = length(v3CameraPos); const float fCameraHeight2 = fCameraHeight * fCameraHeight; const float fm_ESun = 150.0; const float fm_Kr = 0.0025; const float fm_Km = 0.0010; const float fKrESun = fm_Kr * fm_ESun; const float fKmESun = fm_Km * fm_ESun; const float fKr4PI = fm_Kr * 4 * 3.141592653; const float fKm4PI = fm_Km * 4 * 3.141592653; varying vec3 v3Direction; varying vec4 c0, c1; float scale(float fCos) { float x = 1.0 - fCos; return fScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25)))); } void main( void ) { // Get the ray from the camera to the vertex, and its length (which is the far point of the ray passing through the atmosphere) vec3 v3FrontColor = vec3(0.0, 0.0, 0.0); vec3 v3Pos = normalize(gl_Vertex.xyz) * fOuterRadius; vec3 v3Ray = v3CameraPos - v3Pos; float fFar = length(v3Ray); v3Ray = normalize(v3Ray); // Calculate the ray's starting position, then calculate its scattering offset vec3 v3Start = v3CameraPos; float fHeight = length(v3Start); float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fCameraHeight)); float fStartAngle = dot(v3Ray, v3Start) / fHeight; float fStartOffset = fDepth*scale(fStartAngle); // Initialize the scattering loop variables float fSampleLength = fFar / fSamples; float fScaledLength = fSampleLength * fScale; vec3 v3SampleRay = v3Ray * fSampleLength; vec3 v3SamplePoint = v3Start + v3SampleRay * 0.5; // Now loop through the sample rays for(int i=0; i<nSamples; i++) { float fHeight = length(v3SamplePoint); float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight)); float fLightAngle = dot(normalize(v3LightPos), v3SamplePoint) / fHeight; float fCameraAngle = dot(normalize(v3Ray), v3SamplePoint) / fHeight; float fScatter = (-fStartOffset + fDepth*( scale(fLightAngle) - scale(fCameraAngle)))/* 0.25f*/; vec3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI)); v3FrontColor += v3Attenuate * (fDepth * fScaledLength); v3SamplePoint += v3SampleRay; } // Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader vec4 newPos = vec4( (gl_Vertex.xyz + view_position.xyz), 1.0); gl_Position = gl_ModelViewProjectionMatrix * vec4(newPos.xyz, 1.0); gl_Position.z = gl_Position.w * 0.99999; c1 = vec4(v3FrontColor * fKmESun, 1.0); c0 = vec4(v3FrontColor * (v3InvWavelength * fKrESun), 1.0); v3Direction = v3CameraPos - v3Pos; } Fragment Shader: uniform vec3 v3LightPos; varying vec3 v3Direction; varying vec4 c0; varying vec4 c1; const float g =-0.90f; const float g2 = g * g; const float Exposure =2; void main(void){ float fCos = dot(normalize(v3LightPos), v3Direction) / length(v3Direction); float fMiePhase = 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos*fCos) / pow(1.0 + g2 - 2.0*g*fCos, 1.5); gl_FragColor = c0 + fMiePhase * c1; gl_FragColor.a = 1.0; }

    Read the article

  • Mathematics for Computer Science

    - by jiewmeng
    I am going into university next year. I think maths would be one of the more important aspects of computer science? I recently saw the MIT Intro to Algorithms video on YouTube and the maths required is quite hardcore. I wonder what parts of maths do i need, probability, calculus, trigo etc. Will the book Concrete Mathematics - it claims to be foundation for computer science - on Amazon cover most of whats required?

    Read the article

< Previous Page | 7 8 9 10 11 12 13 14 15 16 17 18  | Next Page >