1 vote

Consider two $n \times 1$ vectors $u$ and $v$ , stored as table $U(\text{ind,val})$ and $V(\text{ind,val})$ with the same schema A row $(i,u_i)$ of table $U$ specifies the $i^{th}$ element of vector $u$ has value $u_i$ (similarly for $v$, respectively). Only the non-zero entries of the vectors are stored in the corresponding tables. For example, if the vector $u$ equals $(0, 1, 3, 0, 2, 0)$, then it is represented in table $U$ as $$\begin{array}{|l|l|} \hline \textbf{ind}&\textbf{val} \\\hline 2&1 \\3&3 \\5&2\\ \hline \end{array}$$

Write a relational algebra expression or an SQL query to compute the sum $u + v$ of the two vectors $u$ and $v$. Explain your solution.

Write a relational algebra expression or an SQL query to compute the sum $u + v$ of the two vectors $u$ and $v$. Explain your solution.

2 votes

SINCE THE QUESTION ASKED FOR RELATIONAL ALGEBRA OR SQL EXPRESSION, I AM PROVIDING THE RELATIONAL ALGEBRA EXPRESSION BECAUSE IN ISI MTECH CS EXAM SYLLABUS RELATIONAL ALGEBRA IS THERE.

THE SOLUTION IS AS FOLLOWS:-

SINCE IN QUESTION IT IS MENTIONED THAT VECTOR IS ALREADY STORED IN THE FORM OF TABLE WITH NON ZERO ENTRIES, SO WE WILL NOT WRITING THE EXPRESSION FOR CREATING AND STORING THE TABLE. (IF YOU ARE WRITING SQL QUERY ALSO THEN THERE IS NO NEED OF USING THE CREATE AND INSERT COMMAND). NOW THE SOLUTION IS AS FOLLOWS:-

1) FIRST WE TAKE THE UNION OF TWO RELATIONS U AND V.

2)THEN,WE USE AGGREGATION FUNCTION SUM(val) USING GROUP BY ind.

NOW THE SOLUTION IS ATTACHED BELOW

NOW HOW I WRITE THE SECOND EXPRESSION, BELOW I AM GIVING YOU THE DBMS KORTH BOOK IMAGE WHICH USES THAT SIMILAR KIND OF EXPRESSION

YOU SHOULD ALSO REFER KORTH BOOK FOR THE SECOND EXPRESSION.

0 votes