KAlgebra/Console Tab: Difference between revisions

    From KDE UserBase Wiki
    (A brief tutorial on the KAlgebra Console)
     
    No edit summary
     
    (14 intermediate revisions by 2 users not shown)
    Line 1: Line 1:
    == Getting to Know the Console ==
    <languages />
    <translate>
    == Getting to Know the Console == <!--T:1-->


    The KAlgebra console tab is the first thing you see when you open the KAlgebra program. It is like a calculator, but way better. The small edit field at the very bottom of the screen is the input bar. It is used to input the the problem that you want to solve. The answer is shown in the display area in the middle of the screen.  On the right, there is a medium sized window which is the variable window. This shows the current values of the variables, such as: pi, true, false, and even the current answer. The console can be used to make simple and complex calculations, and it's easier and quicker than bringing up a calculator function. In order to do calculations, you enter the equation you want to be solved into the input bar.  After you press enter it will show you the answer in the display area.
    <!--T:2-->
    The '''KAlgebra''' console tab is the first thing you see when you open the '''KAlgebra''' program. It is like a calculator, but way better. The small edit field at the very bottom of the screen is the input bar. It is used to input the the problem that you want to solve. The answer is shown in the display area in the middle of the screen.  On the right, there is a medium sized window which is the variable window. This shows the current values of the variables, such as: pi, true, false, and even the current answer. The console can be used to make simple and complex calculations, and it's easier and quicker than bringing up a calculator function. In order to do calculations, you enter the equation you want to be solved into the input bar.  After you press <keycap>Enter</keycap> it will show you the answer in the display area.


    === Equals!! or Equals? that is the Question === <!--T:3-->


    == Equals!! or Equals? that is the Question ==
    <!--T:4-->
    One of the main things you must know about '''KAlgebra''' is the difference between ''equals!'' and ''equals?'' I know what you're thinking, “What's the difference?”. Well, "equals!" (:=) is used to set or define the value of the variable you have chosen.  For example, <code>y:=3</code> defines y as equal to 3. ''Equals?'' (=) means you are inquiring as to whether or not a variable is equal to that value. You will receive either the answer true or the answer false in this case. For example, if you input <code>y:= 3</code>, then enter <code>y=2</code>, your answer you will get will be “'''false'''”. So, := is the operator that '''KAlgebra''' uses to ''define'', and = is the ''logical'' operator (that goes along with < > <= and >=).


    One of the main things you must know about KAlgebra is the difference between ''equals!'' and ''equals?'' I know what you're thinking, “What's the difference?”. Well, equals. (''':=''') is used to set or define the value of the variable you have chosen.  For example, '''y:=3''' defines y as equal to 3. ''Equals?'' ('''=''') means you are inquiring as to whether or not a variable is equal to that value. You will receive either the answer true or the answer false in this case. For example, if you input '''y:= 3''', then enter '''y=2''', your answer you will get will be “'''false'''”. So, ''':=''' is the operator that KAlgebra uses to ''define'', and '''=''' is the ''logical'' operator (that goes along with < > <= and >=).
    === KAlgebra as a Kwicker Kalculator === <!--T:5-->


    <!--T:6-->
    There are also basic calculator functions such as, addition (+), subtraction (-), multiplication (*), division (/), exponents (**) and(^),  and order of operations () [Please Excuse My Dear Aunt Sally ;-)]. If you want to take the square root of a number, you type the number followed by ^0.5. For example, the square root of twentyfive: 25^0.5. When using a calculator program on the computer you must mouse around the different virtual key. '''KAlgebra's''' console allows you to just type in what you want to solve which is much faster and more direct.


    == KAlgebra as a Kwicker Kalculator ==
    === Setting up User Functions with the Lambda Operator === <!--T:7-->


    There are also basic calculator functions such as, addition (+), subtraction (-), multiplication (*), division (/), exponents (**) and(^),  and order of operations()[Please Excuse My Dear Aunt Sally ;-)]. If you want to take the square root of a number,you type the number followed by ^0.5. For example, the square root of five: 25^0.5. When using a calculator program on the computer you must mouse around the different virtual key. KAlgebra's console allows you to just type in what you want to solve which is much faster and more direct.
    <!--T:8-->
    The lambda operator is difficult to wrap your head around at first glance. Basically, it is the way to define a new function in the '''KAlgebra''' console. The best way to discuss how to use it is by example. An example of what the area of a rectangle would look like would be:


    <!--T:9-->
    {{Input|1=<nowiki>AreaRect:=(b,h)->b*h </nowiki>}}


    == Setting up User Functions with the Lambda Operator ==
    <!--T:10-->
    What I did here is define (:=) the function AreaRect. AreaRect is a function of two free variables b and h (base and height). The lambda operator (->) shows how they are mapped into the area of a rectangle. After you key this function in, the AreaRect function is built into '''KAlgebra'''. Go ahead, type in, <code>AreaRect(4,2)</code>, you will get the answer, 8.


    The lambda operator is difficult to wrap your head around at first glance. Basically, it is the way to define a new function in the KAlgebra console. The best way to discuss how to use it is by example. An example of what the area of a rectangle would look like would be:
    <!--T:11-->
    Below is a screen shot showing how this most basic of functions is defined, displayed in the variable list, utilized by the user, with results shown in the display.


    <nowiki>AreaRect:=(b,h)->b*h </nowiki>
    <!--T:12-->
    [[File:Kalgebra-lambda-example.png]]


    What I did here is define (:=) the function AreaRect. AreaRect is a function of two free variables b and h (base and height). The lambda operator (->) shows how they are mapped into the area of a rectangle. After you key this function in, the AreaRect function is built into KAlgebra. Go ahead, type in, AreaRect(4,2), you will get the answer, 8.
    === Help! (or is it Help?) === <!--T:13-->


    <!--T:14-->
    The dictionary is the best source for all of '''KAlgebra's''' available functions. You just click the dictionary tab, and you will get a list of all 64 functions. If you want to edit the value of a variable, you can double click on the current value in the variables list and enter anything you want. '''KAlgebra''' includes an auto complete feature which will suggest possible functions you may use for your calculations. It starts working when you begin typing in the input bar. Once you type in at least one letter, it will give you several options as to what you may want to enter. If one of those options is suitable, you click on it, and what you've been typing will be automatically completed.


    === The Ultimate Example of Math Geekery === <!--T:15-->


    == Help! (or is it Help?) ==
    <!--T:16-->
    To show some of the '''KAlgebra''' console concepts, we would like to find the area under a sine curve. To do so, we are going to estimate the area as the area of a bunch of trapezoids that fit under the sine curve. For starters, we define a function (f) that provides our sine curve from 0 to 3.14 radians:


    The dictionary is the best source for all of KAlgebra's available functions. You just click the dictionary tab, and you will get a list of all 64 functions. If you want to edit the value of a variable, you can double click on the current value in the variables list and enter anything you want. KAlgebra includes an auto complete feature which will suggest possible functions you may use for your calculations. It starts working when you begin typing in the input bar. Once you type in at least one letter, it will give you several options as to what you may want to enter. If one of those options is suitable, you click on it, and what you've been typing will be automatically completed.
    <!--T:17-->
    {{Input|1=<nowiki>f:=x->sin(x)</nowiki>}}


    <!--T:18-->
    We didn't have to do this, we could just use sin(x) directly, but we can also replace sin(x) in the definition of f to find the area of other interesting shapes.
    <!--T:19-->
    Next we define a function that is able to calculate the area of a trapezoid:
    <!--T:20-->
    {{Input|1=<nowiki>A:=(b,h1,h2)->0.5*(h1+h2)*b</nowiki>}}
    <!--T:21-->
    The above function is a function of three variables (base, height 1, and height 2) that '''KAlgebra''' is more than capable of handling.
    <!--T:22-->
    Next, we are going to evaluate the series of trapezoids beneath the sine curve in increments of 0.01 radians. To help us do this, we define a helper function x(k)=0.01*k or:
    <!--T:23-->
    {{Input|1=<nowiki>x:=k->0.01*k</nowiki>}}
    <!--T:24-->
    So that as k counts from 0 to 314, x changes from 0 to 3.14 radians. We sum the trapezoid areas up using sum function:
    <!--T:25-->
    {{Input|1=<nowiki>sum(A(.01,f(x(k)),f(x(k+1))): k=0..314)</nowiki>}}
    <!--T:26-->
    This shows off a lot of '''KAlgebra's''' capabilities and syntax. You can see the use of the built in function, sum. We show off the use of nested functions. Also, we see the use of the ''range'' syntax.
    <!--T:27-->
    [[File:Kalgebra-sinearea-example.png]]
    <!--T:28-->
    Our answer is very close to two, which happens to be the exact solution.


    === Recursion === <!--T:29-->


    == The Ultimate Example of Math Geekery ==
    <!--T:30-->
    See, Recursion.


    To show some of the KAlgebra console concepts, we would like to find the area under a sine curve. To do so, we are going to estimate the area as the area of a bunch of trapezoids that fit under the sine curve. For starters, we define a function (f) that provides our sine curve from 0 to 3.14 radians:
    <!--T:31-->
    OK, seriously, to understand recursion, you must first understand recursion. The '''KAlgebra''' console provides the user with a virtual laboratory for experimenting with and using recursion. Probably the most used example of recursion is the factorial. Below we define our own factorial function using '''KAlgebra's''' capabilities for defining functions, performing logic inside of functions, and having a function call itself, which is the definition of recursion:


    <nowiki>f:=x->sin(x)</nowiki>
    <!--T:32-->
    {{Input|1=<nowiki>fact:=n->piecewise{ n<=1 ? 1, ? n*fact(n-1)}</nowiki>}}


    We didn't have to do this, we could just use sin(x) directly, but we can also replace sin(x) in the definition of f to find the area of other interesting shapes.
    <!--T:33-->
    So, breaking this down, we're defining our own function (fact) which just so happens to call fact. This takes advantage of the recursive definition of the factorial function.
     
    <!--T:34-->
    fact(n) = n! = n*(n-1)*(n-2)*...*2*1
     
    <!--T:35-->
    Noting that (n-1)! = (n-1)*(n-2)*...*2*1,
    we have: n! = n*(n-1)!
    or fact(n) = n*fact(n-1)
     
    <!--T:36-->
    To stop the chain of calculations, we define that 1! = 1. We accomplish this bit of logic using the piecewise{ expression ? value, expression ? value, ..., ? default value} construct.
     
    <!--T:37-->
    [[File:Kalgebra-recursion-example.png]]
     
    === Fun with Lists === <!--T:38-->
     
    <!--T:39-->
    Beginning with version 0.11, '''KAlgebra''' comes with advanced (and by advanced, I mean time saving) list operations. '''KAlgebra''' had previously come with the means of entering a list of variables or values. For example, one could enter a list of numbers as follows:
     
    <!--T:40-->
    {{Input|1=<nowiki>x:=list{0,.2,.4,.6,.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3.0,3.2}</nowiki>}}
     
    <!--T:41-->
    Now, with the '''map''' function, '''KAlgebra''' provides the user with the means of applying a function to each element of the list. As a simple example, one might wish to evaluate the sine of each element of the list. One might also be tempted to enter '''sin(x)'''. One would be disappointed.
     
    <!--T:42-->
    To apply a function to each element of the list we use the map function as follows:
     
    <!--T:43-->
    {{Input|1=<nowiki>s := map( u->sin(u), x)</nowiki>}}


    Next we define a function that is able to calculate the area of a trapezoid:
    <!--T:44-->
    The first argument of the map function sets up the function that will be applied to all elements of the list given in the second argument of the map function. So now, '''s''' is a list holding the sine of every element of x.


    <nowiki>A:=(b,h1,h2)->0.5*(h1+h2)*b</nowiki>
    <!--T:45-->
    If we want to pare down a list we can use the filter function to select the list elements we want to keep:


    The above function is a function of three variables (base, height 1, and height 2) that KAlgebra is more than capable of handling.
    <!--T:46-->
    {{Input|1=<nowiki>f := filter( u-> and(u>=-0.5,u<=0.5), s)</nowiki>}}


    Next, we are going to evaluate the series of trapezoids beneath the sine curve in increments of 0.01 radians. To help us do this, we define a helper function x(k)=0.01*k or:
    <!--T:47-->
    After this, f will contain only elements of s that are between -0.5 and +0.5. The first argument of the filter function defines a logical operation used to select the values of the list that we want.


    <nowiki>x:=k->0.01*k</nowiki>
    <!--T:48-->
    Below is a screenshot of the various list operations discussed:


    So that as k counts from 0 to 314, x changes from 0 to 3.14 radians. We sum the trapezoid areas up using sum function:
    <!--T:49-->
    [[File:Kalgebra-listopsoxy-example.png]]


    <nowiki>sum(A(.01,f(x(k)),f(x(k+1))): k=0..314)</nowiki>


    This shows off a lot of KAlgebra's capabilities and syntax. You can see the use of the built in function, sum. We show off the use of nested functions. Also, we see the use of the ''range'' syntax.
    === Parting Words === <!--T:50-->


    Our answer is very close to two, which happens to be the exact solution.
    <!--T:51-->
    We have scratched the surface of the wonders presented by the '''KAlgebra''' user console. There is a lot of stuff that can be done, so roll up your sleeves, and do some math!


    <!--T:52-->
    --[[User:Trlanzi|trLanzi]] 01:10, 1 December 2011 (UTC)
    --[[User:Trlanzi|trLanzi]] 01:10, 1 December 2011 (UTC)
    <!--T:53-->
    [[Category:Education]]
    [[Category:Tutorials]]
    </translate>

    Latest revision as of 13:25, 3 October 2012

    Other languages:

    Getting to Know the Console

    The KAlgebra console tab is the first thing you see when you open the KAlgebra program. It is like a calculator, but way better. The small edit field at the very bottom of the screen is the input bar. It is used to input the the problem that you want to solve. The answer is shown in the display area in the middle of the screen. On the right, there is a medium sized window which is the variable window. This shows the current values of the variables, such as: pi, true, false, and even the current answer. The console can be used to make simple and complex calculations, and it's easier and quicker than bringing up a calculator function. In order to do calculations, you enter the equation you want to be solved into the input bar. After you press Enter it will show you the answer in the display area.

    Equals!! or Equals? that is the Question

    One of the main things you must know about KAlgebra is the difference between equals! and equals? I know what you're thinking, “What's the difference?”. Well, "equals!" (:=) is used to set or define the value of the variable you have chosen. For example, y:=3 defines y as equal to 3. Equals? (=) means you are inquiring as to whether or not a variable is equal to that value. You will receive either the answer true or the answer false in this case. For example, if you input y:= 3, then enter y=2, your answer you will get will be “false”. So, := is the operator that KAlgebra uses to define, and = is the logical operator (that goes along with < > <= and >=).

    KAlgebra as a Kwicker Kalculator

    There are also basic calculator functions such as, addition (+), subtraction (-), multiplication (*), division (/), exponents (**) and(^), and order of operations () [Please Excuse My Dear Aunt Sally ;-)]. If you want to take the square root of a number, you type the number followed by ^0.5. For example, the square root of twentyfive: 25^0.5. When using a calculator program on the computer you must mouse around the different virtual key. KAlgebra's console allows you to just type in what you want to solve which is much faster and more direct.

    Setting up User Functions with the Lambda Operator

    The lambda operator is difficult to wrap your head around at first glance. Basically, it is the way to define a new function in the KAlgebra console. The best way to discuss how to use it is by example. An example of what the area of a rectangle would look like would be:

    AreaRect:=(b,h)->b*h 

    What I did here is define (:=) the function AreaRect. AreaRect is a function of two free variables b and h (base and height). The lambda operator (->) shows how they are mapped into the area of a rectangle. After you key this function in, the AreaRect function is built into KAlgebra. Go ahead, type in, AreaRect(4,2), you will get the answer, 8.

    Below is a screen shot showing how this most basic of functions is defined, displayed in the variable list, utilized by the user, with results shown in the display.

    Help! (or is it Help?)

    The dictionary is the best source for all of KAlgebra's available functions. You just click the dictionary tab, and you will get a list of all 64 functions. If you want to edit the value of a variable, you can double click on the current value in the variables list and enter anything you want. KAlgebra includes an auto complete feature which will suggest possible functions you may use for your calculations. It starts working when you begin typing in the input bar. Once you type in at least one letter, it will give you several options as to what you may want to enter. If one of those options is suitable, you click on it, and what you've been typing will be automatically completed.

    The Ultimate Example of Math Geekery

    To show some of the KAlgebra console concepts, we would like to find the area under a sine curve. To do so, we are going to estimate the area as the area of a bunch of trapezoids that fit under the sine curve. For starters, we define a function (f) that provides our sine curve from 0 to 3.14 radians:

    f:=x->sin(x)

    We didn't have to do this, we could just use sin(x) directly, but we can also replace sin(x) in the definition of f to find the area of other interesting shapes.

    Next we define a function that is able to calculate the area of a trapezoid:

    A:=(b,h1,h2)->0.5*(h1+h2)*b

    The above function is a function of three variables (base, height 1, and height 2) that KAlgebra is more than capable of handling.

    Next, we are going to evaluate the series of trapezoids beneath the sine curve in increments of 0.01 radians. To help us do this, we define a helper function x(k)=0.01*k or:

    x:=k->0.01*k

    So that as k counts from 0 to 314, x changes from 0 to 3.14 radians. We sum the trapezoid areas up using sum function:

    sum(A(.01,f(x(k)),f(x(k+1))): k=0..314)

    This shows off a lot of KAlgebra's capabilities and syntax. You can see the use of the built in function, sum. We show off the use of nested functions. Also, we see the use of the range syntax.

    Our answer is very close to two, which happens to be the exact solution.

    Recursion

    See, Recursion.

    OK, seriously, to understand recursion, you must first understand recursion. The KAlgebra console provides the user with a virtual laboratory for experimenting with and using recursion. Probably the most used example of recursion is the factorial. Below we define our own factorial function using KAlgebra's capabilities for defining functions, performing logic inside of functions, and having a function call itself, which is the definition of recursion:

    fact:=n->piecewise{ n<=1 ? 1, ? n*fact(n-1)}

    So, breaking this down, we're defining our own function (fact) which just so happens to call fact. This takes advantage of the recursive definition of the factorial function.

    fact(n) = n! = n*(n-1)*(n-2)*...*2*1

    Noting that (n-1)! = (n-1)*(n-2)*...*2*1, we have: n! = n*(n-1)! or fact(n) = n*fact(n-1)

    To stop the chain of calculations, we define that 1! = 1. We accomplish this bit of logic using the piecewise{ expression ? value, expression ? value, ..., ? default value} construct.

    Fun with Lists

    Beginning with version 0.11, KAlgebra comes with advanced (and by advanced, I mean time saving) list operations. KAlgebra had previously come with the means of entering a list of variables or values. For example, one could enter a list of numbers as follows:

    x:=list{0,.2,.4,.6,.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3.0,3.2}

    Now, with the map function, KAlgebra provides the user with the means of applying a function to each element of the list. As a simple example, one might wish to evaluate the sine of each element of the list. One might also be tempted to enter sin(x). One would be disappointed.

    To apply a function to each element of the list we use the map function as follows:

    s := map( u->sin(u), x)

    The first argument of the map function sets up the function that will be applied to all elements of the list given in the second argument of the map function. So now, s is a list holding the sine of every element of x.

    If we want to pare down a list we can use the filter function to select the list elements we want to keep:

    f := filter( u-> and(u>=-0.5,u<=0.5), s)

    After this, f will contain only elements of s that are between -0.5 and +0.5. The first argument of the filter function defines a logical operation used to select the values of the list that we want.

    Below is a screenshot of the various list operations discussed:


    Parting Words

    We have scratched the surface of the wonders presented by the KAlgebra user console. There is a lot of stuff that can be done, so roll up your sleeves, and do some math!

    --trLanzi 01:10, 1 December 2011 (UTC)