-
Notifications
You must be signed in to change notification settings - Fork 5
Vector operation koans #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
camilo1704
wants to merge
6
commits into
master
Choose a base branch
from
vector-operations-koans
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
28efeef
vertor operation koans added
camilo1704 0c736e4
other tests added
camilo1704 b12dca4
some text with information added
camilo1704 5103206
minor fix
camilo1704 e9a9577
Merge branch 'master' into vector-operations-koans
camilo1704 1649d6f
tests added to tests_solutions
camilo1704 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,90 @@ | ||
module VectorOperationKoans | ||
using LinearAlgebra | ||
|
||
#= | ||
Vectors are a commmon data structure containers present in most common programming | ||
languages. Particularly in Julia, they are represented as a 1D Array. Also, basic linear algebra | ||
operations are useful for common needed calculations such as norms and distances. | ||
Here is some useful documentation for arrays (and vectors) in Julia and the LinearAlgebra module: | ||
- https://docs.julialang.org/en/v1/base/arrays/ | ||
- https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/ | ||
=# | ||
|
||
function vector_length(vec) | ||
#= | ||
Return the length of the vector "vec". | ||
Ex: vec = [ 9, 5, 6]; return 3. | ||
=# | ||
end | ||
|
||
function range_1_to_n(n) | ||
#= | ||
Return a vector with ordered elements from 1 to n. | ||
Ex: n = 5; return [1, 2, 3, 4, 5]. | ||
=# | ||
end | ||
|
||
function vector_of_zeros(n) | ||
#= | ||
Return a vector with n zero elements. | ||
Ex: n = 4; return [0, 0, 0, 0]. | ||
=# | ||
end | ||
|
||
function vector_of_ones(n) | ||
#= | ||
Return a vector with n ones. | ||
Ex: n = 3; return [1, 1, 1]. | ||
=# | ||
end | ||
|
||
function elementwise_sum(x, y) | ||
#= | ||
Return the element-wise sum of the vectors x and y. | ||
Ex: x = [2, 4, 6]; y = [8, 0, -1]; return [10, 4, 5]. | ||
=# | ||
end | ||
|
||
function elementwise_substraction(x, y) | ||
#= | ||
Return the element-wise substraction of the vectors x and y. | ||
Ex: x = [2, 4, 6]; y = [8, 0, -1]; return [-6, 4, 7]. | ||
=# | ||
end | ||
|
||
function scalar_vector_addition(vec, k) | ||
#= | ||
Return the outcome of adding the scalar k to the vector vec. | ||
Ex: k = 2; vec = [3, 4, 5]; return [5, 6, 7]. | ||
=# | ||
end | ||
|
||
function scalar_vector_multiplication(vec, k) | ||
#= | ||
Return the outcome of multiplying the scalar k to the vector vec. | ||
Ex: k = 2; vec = [3, 4, 5]; return [6, 8, 10]. | ||
=# | ||
end | ||
|
||
function scalar_product(x, y) | ||
#= | ||
Return the scalar or dot product of x and y. | ||
Ex: x = [2, 4, 6]; y = [3, 7, 8]; return 82 (2*3 + 4*7 + 8*6). | ||
=# | ||
end | ||
|
||
function norm_l2(x) | ||
#= | ||
Return the norm L2 of the vector x. | ||
Ex: x = [4, -3, 7]; return ≈ 8.6 (sqrt(4*4 + -3*-3 + 7*7)) | ||
=# | ||
end | ||
|
||
function euclidean_distance(x, y) | ||
#= | ||
Return the euclidean distance between x and y. | ||
Ex: x = [1, 2, 4]; y = [ 5, 6, 7]; return ≈ 6.4 (sqrt( (1-5)^2 + (2-6)^2 + (4-7)^2 )) | ||
=# | ||
end | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
module VectorOperationKoans | ||
using LinearAlgebra | ||
|
||
#= | ||
Vectors are a commmon data structure containers present in most common programming | ||
languages. Particularly in Julia, they are represented as a 1D Array. Also, basic linear algebra | ||
operations are useful for common needed calculations such as norms and distances. | ||
Here is some useful documentation for arrays (and vectors) in Julia and the LinearAlgebra module: | ||
- https://docs.julialang.org/en/v1/base/arrays/ | ||
- https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/ | ||
=# | ||
|
||
function vector_length(vec) | ||
#= | ||
Return the length of the vector "vec". | ||
Ex: vec = [ 9, 5, 6]; return 3. | ||
=# | ||
return length(vec) | ||
end | ||
|
||
function range_1_to_n(n) | ||
#= | ||
Return a vector with ordered elements from 1 to n. | ||
Ex: n = 5; return [1, 2, 3, 4, 5]. | ||
=# | ||
return collect(1:n) | ||
end | ||
|
||
function vector_of_zeros(n) | ||
#= | ||
Return a vector with n zero elements. | ||
Ex: n = 4; return [0, 0, 0, 0]. | ||
=# | ||
return zeros(n) | ||
end | ||
|
||
function vector_of_ones(n) | ||
#= | ||
Return a vector with n ones. | ||
Ex: n = 3; return [1, 1, 1]. | ||
=# | ||
return ones(n) | ||
end | ||
|
||
function elementwise_sum(x, y) | ||
#= | ||
Return the element-wise sum of the vectors x and y. | ||
Ex: x = [2, 4, 6]; y = [8, 0, -1]; return [10, 4, 5]. | ||
=# | ||
return x+y | ||
end | ||
|
||
function elementwise_substraction(x, y) | ||
#= | ||
Return the element-wise substraction of the vectors x and y. | ||
Ex: x = [2, 4, 6]; y = [8, 0, -1]; return [-6, 4, 7]. | ||
=# | ||
return x-y | ||
end | ||
|
||
function scalar_vector_addition(vec, k) | ||
#= | ||
Return the outcome of adding the scalar k to the vector vec. | ||
Ex: k = 2; vec = [3, 4, 5]; return [5, 6, 7]. | ||
=# | ||
return vec.+k | ||
end | ||
|
||
function scalar_vector_multiplication(vec, k) | ||
#= | ||
Return the outcome of multiplying the scalar k to the vector vec. | ||
Ex: k = 2; vec = [3, 4, 5]; return [6, 8, 10]. | ||
=# | ||
return k*vec | ||
end | ||
|
||
function scalar_product(x, y) | ||
#= | ||
Return the scalar or dot product of x and y. | ||
Ex: x = [2, 4, 6]; y = [3, 7, 8]; return 82 (2*3 + 4*7 + 8*6). | ||
=# | ||
return dot(x, y) | ||
end | ||
|
||
function norm_l2(x) | ||
#= | ||
Return the norm L2 of the vector x. | ||
Ex: x = [4, -3, 7]; return ≈ 8.6 (sqrt(4*4 + -3*-3 + 7*7)) | ||
=# | ||
norm(x) | ||
end | ||
|
||
function euclidean_distance(x, y) | ||
#= | ||
Return the euclidean distance between x and y. | ||
Ex: x = [1, 2, 4]; y = [ 5, 6, 7]; return ≈ 6.4 (sqrt( (1-5)^2 + (2-6)^2 + (4-7)^2 )) | ||
=# | ||
norm(x-y) | ||
end | ||
|
||
end |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These exercises would really benefit from having a small description of the problem to solve, along with a few examples for each.