Created
December 13, 2014 02:17
-
-
Save jasonLaster/412911fe7e94389b4474 to your computer and use it in GitHub Desktop.
Calculating the norm of a vector in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"title":"Calculating the norm of a vector in Javascript","author":"jmeas","pages":[{"pageName":"","sections":[{"type":"text","source":"Determining the norm of a vector can be daunting :fearful: But after you've read this exercise you'll be a pro at it :+1:\n\nThe norm of a vector is its length. In two dimensions, the norm of a vector \\(\\vec{v}\\) can be calculated with the following equation:\n\n$$\\lvert v \\rvert = \\sqrt{x^2+y^2}$$\n\nIn Javascript, we could write a function to calculate this like so:"},{"type":"javascript","source":"var math = require('mathjs');\n\nfunction norm(x, y) {\n return math.sqrt(math.square(x) + math.square(y));\n}"},{"type":"text","source":"Let's take a look at how we can use this to calculate the norm of a vector \\(\\vec{v} = [3, 4]\\)"},{"type":"javascript","source":"var val = norm(3, 4);\n\n// Set it to the output div\ndocument.getElementsByClassName('output')[0].innerHTML = val;"},{"type":"html","source":"<!-- This is a div to show our output -->\n<div>The norm of our vector is: <span class='output'></span></div>"},{"type":"css","source":"/* Let's give it some styling, too */\ndiv {\n background: #fafafa;\n border: 1px solid #ddd;\n padding: 20px;\n}"}]}],"public":true} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment